package com.hskn.hss.module.carPlan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.annotation.Excel;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.exception.BaseException;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.bigscreen.entity.vo.JxqsKaoQin;
import com.hskn.hss.module.bigscreen.mapper.BigScreenMapper;
import com.hskn.hss.module.carPlan.to.CarPlanTo;
import com.hskn.hss.module.carPlan.to.CarWorkPlanCountTo;
import com.hskn.hss.module.carPlan.vo.*;
import com.hskn.hss.module.carPlanCar.entity.CarPlanCar;
import com.hskn.hss.module.carPlanCar.mapper.CarPlanCarMapper;
import com.hskn.hss.module.carPlanCollectPoint.entity.CarPlanCollectPoint;
import com.hskn.hss.module.carPlanCollectPoint.mapper.CarPlanCollectPointMapper;
import com.hskn.hss.module.carPlanCollectStand.entity.CarPlanCollectStand;
import com.hskn.hss.module.carPlanCollectStand.mapper.CarPlanCollectStandMapper;
import com.hskn.hss.module.carPlanProcessStand.entity.CarPlanProcessStand;
import com.hskn.hss.module.carPlanProcessStand.mapper.CarPlanProcessStandMapper;
import com.hskn.hss.module.carTrack.entity.CarTrack;
import com.hskn.hss.module.carTrack.mapper.CarTrackMapper;
import com.hskn.hss.module.carPlan.entity.CarPlan;
import com.hskn.hss.module.carPlan.mapper.CarPlanMapper;
import com.hskn.hss.module.carPlan.service.CarPlanService;
import com.hskn.hss.module.carTrack.vo.FacilitiesListVo;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.elementtotal.service.ElementTotalService;
import com.hskn.hss.module.elementtotal.vo.FaclSearchListVo;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.gridSnapshot.entity.GridSnapshot;
import com.hskn.hss.module.gridSnapshot.mapper.GridSnapshotMapper;
import com.hskn.hss.module.jobset.entity.Jobset;
import com.hskn.hss.module.jobset.mapper.JobsetMapper;
import com.hskn.hss.module.oilDevice.domain.StateOilDevice;
import com.hskn.hss.module.oilDevice.mapper.StateOilDeviceMapper;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.rptCarWorkDetail.entity.RptCarWorkDetail;
import com.hskn.hss.module.rptCarWorkDetail.mapper.RptCarWorkDetailMapper;
import com.hskn.hss.module.rptCarWorkFacility.entity.RptCarWorkFacility;
import com.hskn.hss.module.rptCarWorkFacility.mapper.RptCarWorkFacilityMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlchannel.entity.TlChannel;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.mapper.TlOperationMapper;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.module.workgroup.mapper.WorkGroupMapper;
import com.hskn.hss.module.workkaoqin.entity.WorkKaoqin;
import com.hskn.hss.module.workkaoqin.mapper.WorkKaoqinMapper;
import com.hskn.hss.module.workrule.entity.WorkRule;
import com.hskn.hss.module.workrule.mapper.WorkRuleMapper;
import com.hskn.hss.module.worktype.entity.WorkType;
import com.hskn.hss.module.worktype.mapper.WorkTypeMapper;
import com.hskn.hss.utils.*;
import com.hskn.hss.utils.page.PageVo;
import com.hskn.hss.utils.redis.RedisUtil;
import com.hskn.hss.utils.redis.key.CarAlarmMaxSpeedKeyToday;
import com.hskn.hss.utils.redis.key.CarTrackLastOneKeyToday;
import com.hskn.hss.utils.redis.key.RptCarWorkDetailLastOneKeyToday;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@Service
public class CarPlanServiceImpl extends ServiceImpl<CarPlanMapper, CarPlan> implements CarPlanService {

    @Autowired
    private CarPlanMapper carPlanMapper;
    @Autowired
    private CarPlanCarMapper carPlanCarMapper;
    @Autowired
    ElementTotalService elementTotalService;
    @Resource
    GridMapper gridMapper;
    @Resource
    private TlCarMapper tlCarMapper;
    @Resource
    private WorkGroupMapper workGroupMapper;
    @Resource
    private RptCarWorkMapper rptCarWorkMapper;
    @Resource
    private SysDepartMapper sysDepartMapper;
    @Resource
    private CarTrackMapper carTrackMapper;
    @Autowired
    LocationUtils locationUtil;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private JobsetMapper jobsetMapper;
    @Autowired
    ISysDepartService departService;
    @Autowired
    private GridSnapshotMapper gridSnapshotMapper;
    @Autowired
    private WorkRuleMapper workRuleMapper;
    @Autowired
    private CarPlanCollectPointMapper carPlanCollectPointMapper;
    @Autowired
    private TlFacilitiesMapper tlFacilitiesMapper;
    @Autowired
    private CarPlanCollectStandMapper carPlanCollectStandMapper;
    @Autowired
    private CarPlanProcessStandMapper carPlanProcessStandMapper;
    @Autowired
    private TlOperationMapper tlOperationMapper;
    @Autowired
    private WorkKaoqinMapper workKaoqinMapper;
    @Autowired
    private BigScreenMapper bigScreenMapper;
    @Autowired
    private RptCarWorkFacilityMapper rptCarWorkFacilityMapper;
    @Autowired
    private RptCarWorkDetailMapper rptCarWorkDetailMapper;
    @Autowired
    private StateOilDeviceMapper stateOilDeviceMapper;


    @Override
    public PageVo mechanicalList(CarPlanTo carPlanTo) {

        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getName()).collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));


        List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));


        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carPlanTo.getWorkTypeId())) {
            carPlanQueryWrapper.lambda().eq(CarPlan::getWorkTypeId, carPlanTo.getWorkTypeId());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getPlanName())) {
            carPlanQueryWrapper.lambda().like(CarPlan::getPlanName, carPlanTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getDepartmentId())) {
            List<String> departmentList = departService.getChildIdsByIId(carPlanTo.getDepartmentId());
            carPlanQueryWrapper.lambda().in(CarPlan::getDepartmentId, departmentList);
        }
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, 1);
        carPlanQueryWrapper.lambda().orderByDesc(CarPlan::getCreateTime);
        List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);

        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
        Map<String, List<CarPlanCar>> carPlanCarMap = carPlanCarList.stream().collect(Collectors.groupingBy(CarPlanCar::getCarPlanId));


        for (CarPlan carPlan : carPlanList) {
            WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
            if (null != workGroup) {
                carPlan.setWorkGroupName(workGroup.getName());
            }
            WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
            if (null != workRule) {
                carPlan.setWorkRuleName(workRule.getRuleName());
            }
            Grid grid = gridMap.get(carPlan.getGridId());
            if (null != grid) {
                carPlan.setGridName(grid.getName());
            }
            TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
            if (null != tlOperation) {
                carPlan.setWorkTypeName(tlOperation.getOperationtype());
            }
            SysDepart sysDepart = sysDepartMap.get(carPlan.getDepartmentId());
            if (null != sysDepart) {
                carPlan.setDepartmentName(sysDepart.getDepartName());
            }
            List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
            Set<String> carIdsList = new HashSet<>();
            Set<String> carNumberList = new HashSet<>();
            if (null != carPlanCarList1) {
                for (CarPlanCar carPlanCar : carPlanCarList1) {
                    TlCar tlCar = carMap.get(carPlanCar.getCarId());
                    if (null != tlCar) {
                        carIdsList.add(tlCar.getId());
                        carNumberList.add(tlCar.getCarnumber());
                    }
                }
            }
            carPlan.setCarIds(String.join(",", carIdsList));
            carPlan.setCarNumbers(String.join(",", carNumberList));
        }

        if (null != carPlanTo.getCarIds() && !carPlanTo.getCarIds().isEmpty()) {
            String carIds = String.join(",", carPlanTo.getCarIds());
            List<CarPlan> carPlanListFilterCarNumber = carPlanList.stream().filter(carPlan -> null != carPlan.getCarIds() && carPlan.getCarIds().contains(carIds)).collect(toList());
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanListFilterCarNumber);
        } else {
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanList);
        }

    }

    @Override
    public PageVo trashPickupList(CarPlanTo carPlanTo) {

        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getName()).collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carPlanTo.getPlanName())) {
            carPlanQueryWrapper.lambda().like(CarPlan::getPlanName, carPlanTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getDepartmentId())) {
            List<String> departmentList = departService.getChildIdsByIId(carPlanTo.getDepartmentId());

            carPlanQueryWrapper.lambda().in(CarPlan::getDepartmentId, departmentList);
        }
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, 2);
        carPlanQueryWrapper.lambda().orderByDesc(CarPlan::getCreateTime);
        List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);

        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
        Map<String, List<CarPlanCar>> carPlanCarMap = carPlanCarList.stream().collect(Collectors.groupingBy(CarPlanCar::getCarPlanId));

        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
        Map<String, List<CarPlanCollectPoint>> carPlanCollectPointMap = carPlanCollectPointList.stream().collect(Collectors.groupingBy(CarPlanCollectPoint::getCarPlanId));

        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        for (CarPlan carPlan : carPlanList) {
            WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
            if (null != workGroup) {
                carPlan.setWorkGroupName(workGroup.getName());
            }
            WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
            if (null != workRule) {
                carPlan.setWorkRuleName(workRule.getRuleName());
            }
            Grid grid = gridMap.get(carPlan.getGridId());
            if (null != grid) {
                carPlan.setGridName(grid.getName());
            }
            TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
            if (null != tlOperation) {
                carPlan.setWorkTypeName(tlOperation.getOperationtype());
            }
            SysDepart sysDepart = sysDepartMap.get(carPlan.getDepartmentId());
            if (null != sysDepart) {
                carPlan.setDepartmentName(sysDepart.getDepartName());
            }
            List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
            Set<String> carIdsList = new HashSet<>();
            Set<String> carNumberList = new HashSet<>();
            if (null != carPlanCarList1) {
                for (CarPlanCar carPlanCar : carPlanCarList1) {
                    TlCar tlCar = carMap.get(carPlanCar.getCarId());
                    if (null != tlCar) {
                        carIdsList.add(tlCar.getId());
                        carNumberList.add(tlCar.getCarnumber());
                    }
                }
            }
            carPlan.setCarIds(String.join(",", carIdsList));
            carPlan.setCarNumbers(String.join(",", carNumberList));

            List<CarPlanCollectPoint> carPlanCollectPointList1 = carPlanCollectPointMap.get(carPlan.getId());
            List<String> facNameList = new ArrayList<>();
            if (null != carPlanCollectPointList1) {
                for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectPoint.getCollectPointId());
                    if (null != tlFacilities) {
                        facNameList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setCollectPointSize(facNameList.size());
        }

        if (null != carPlanTo.getCarIds() && !carPlanTo.getCarIds().isEmpty()) {
            String carIds = String.join(",", carPlanTo.getCarIds());
            List<CarPlan> carPlanListFilterCarNumber = carPlanList.stream().filter(carPlan -> null != carPlan.getCarIds() && carPlan.getCarIds().contains(carIds)).collect(toList());
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanListFilterCarNumber);
        } else {
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanList);
        }

    }

    @Override
    public PageVo garbageTransitList(CarPlanTo carPlanTo) {

        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getName()).collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carPlanTo.getPlanName())) {
            carPlanQueryWrapper.lambda().like(CarPlan::getPlanName, carPlanTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getDepartmentId())) {
            List<String> departmentList = departService.getChildIdsByIId(carPlanTo.getDepartmentId());
            carPlanQueryWrapper.lambda().in(CarPlan::getDepartmentId, departmentList);
        }
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, 3);
        carPlanQueryWrapper.lambda().orderByDesc(CarPlan::getCreateTime);
        List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);

        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
        Map<String, List<CarPlanCar>> carPlanCarMap = carPlanCarList.stream().collect(Collectors.groupingBy(CarPlanCar::getCarPlanId));

        QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
        Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap = carPlanCollectStandList.stream().collect(Collectors.groupingBy(CarPlanCollectStand::getCarPlanId));

        QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
        Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap = carPlanProcessStandList.stream().collect(Collectors.groupingBy(CarPlanProcessStand::getCarPlanId));


        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        for (CarPlan carPlan : carPlanList) {
            WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
            if (null != workGroup) {
                carPlan.setWorkGroupName(workGroup.getName());
            }
            WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
            if (null != workRule) {
                carPlan.setWorkRuleName(workRule.getRuleName());
            }
            Grid grid = gridMap.get(carPlan.getGridId());
            if (null != grid) {
                carPlan.setGridName(grid.getName());
            }
            TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
            if (null != tlOperation) {
                carPlan.setWorkTypeName(tlOperation.getOperationtype());
            }
            SysDepart sysDepart = sysDepartMap.get(carPlan.getDepartmentId());
            if (null != sysDepart) {
                carPlan.setDepartmentName(sysDepart.getDepartName());
            }
            List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
            Set<String> carIdsList = new HashSet<>();
            Set<String> carNumberList = new HashSet<>();
            if (null != carPlanCarList1) {
                for (CarPlanCar carPlanCar : carPlanCarList1) {
                    TlCar tlCar = carMap.get(carPlanCar.getCarId());
                    if (null != tlCar) {
                        carIdsList.add(tlCar.getId());
                        carNumberList.add(tlCar.getCarnumber());
                    }
                }
            }
            carPlan.setCarIds(String.join(",", carIdsList));
            carPlan.setCarNumbers(String.join(",", carNumberList));

            List<CarPlanCollectStand> carPlanCollectStandList1 = carPlanCollectStandMap.get(carPlan.getId());
            Set<String> collectStandList = new HashSet<>();
            if (null != carPlanCollectStandList1) {
                for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectStand.getCollectStandId());
                    if (null != tlFacilities) {
                        collectStandList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setCollectStandNames(String.join(",", collectStandList));

            List<CarPlanProcessStand> carPlanProcessStandList1 = carPlanProcessStandMap.get(carPlan.getId());
            Set<String> processStandList = new HashSet<>();
            if (null != carPlanProcessStandList1) {
                for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanProcessStand.getProcessStandId());
                    if (null != tlFacilities) {
                        processStandList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setProcessStandNames(String.join(",", processStandList));
        }

        if (null != carPlanTo.getCarIds() && !carPlanTo.getCarIds().isEmpty()) {
            String carIds = String.join(",", carPlanTo.getCarIds());
            List<CarPlan> carPlanListFilterCarNumber = carPlanList.stream().filter(carPlan -> null != carPlan.getCarIds() && carPlan.getCarIds().contains(carIds)).collect(toList());
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanListFilterCarNumber);
        } else {
            return Tools.page(carPlanTo.getPage(), carPlanTo.getSize(), carPlanList);
        }

    }

    @Override
    public CarPlanMessageVo create(CarPlanTo carPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<Grid> gridListOne = gridMapper.selectList(new QueryWrapper<>());
            Map<String, Grid> gridMap = gridListOne.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

            List<String> gridList = carPlanTo.getGridIdList();
            if (null != gridList) {
                for (String gridId : gridList) {
                    CarPlan carPlan = new CarPlan();
                    carPlan.setId(StringUtils.uuid());

                    carPlan.setCreateTime(new Date());
                    if (carPlanTo.getModule() == 1) {
                        Grid grid1 = gridMap.get(gridId);
                        if (null != grid1) {
                            carPlan.setPlanName(carPlanTo.getPlanName() + "-" + grid1.getName());
                        }
                    } else {
                        carPlan.setPlanName(carPlanTo.getPlanName());
                    }

                    carPlan.setGridId(gridId);
                    carPlan.setDepartmentId(carPlanTo.getDepartmentId());
                    carPlan.setWorkTypeId(carPlanTo.getWorkTypeId());
                    carPlan.setWorkGroupId(carPlanTo.getWorkGroupId());
                    carPlan.setWorkRuleId(carPlanTo.getWorkRuleId());
                    carPlan.setWorkCycle(carPlanTo.getWorkCycle());
                    carPlan.setWorkTrip(carPlanTo.getWorkTrip());
                    carPlan.setExpectOilConsumption(carPlanTo.getExpectOilConsumption());
                    carPlan.setExpectWaterConsumption(carPlanTo.getExpectWaterConsumption());
                    carPlan.setExpectTimeConsumption(carPlanTo.getExpectTimeConsumption());
                    carPlan.setExpectMileage(carPlanTo.getExpectMileage());
                    carPlan.setWorkStartDate(carPlanTo.getWorkStartDate());
                    carPlan.setWorkEndDate(carPlanTo.getWorkEndDate());
                    carPlan.setIsEnable(carPlanTo.getIsEnable());
                    carPlan.setRemark(carPlanTo.getRemark());
                    carPlan.setWorkType(carPlanTo.getWorkType());
                    carPlan.setType(carPlanTo.getType());

                    carPlanMapper.insert(carPlan);


                    Set<String> carIds = carPlanTo.getCarIdList();
                    for (String carId : carIds) {
                        CarPlanCar carPlanCar = new CarPlanCar();
                        carPlanCar.setCarPlanId(carPlan.getId());
                        carPlanCar.setCarId(carId);
                        carPlanCarMapper.insert(carPlanCar);
                    }
                    if (null != carPlanTo.getCarPlanCollectPointIdList() && !carPlanTo.getCarPlanCollectPointIdList().isEmpty()) {
                        Set<String> collectPointList = carPlanTo.getCarPlanCollectPointIdList();
                        for (String collectPointId : collectPointList) {
                            CarPlanCollectPoint carPlanCollectPoint = new CarPlanCollectPoint();
                            carPlanCollectPoint.setCarPlanId(carPlan.getId());
                            carPlanCollectPoint.setCollectPointId(collectPointId);
                            carPlanCollectPointMapper.insert(carPlanCollectPoint);
                        }
                    }
                }
            } else {
                CarPlan carPlan = new CarPlan();
                carPlan.setId(StringUtils.uuid());
                carPlan.setCreateTime(new Date());
                carPlan.setPlanName(carPlanTo.getPlanName());
                carPlan.setDepartmentId(carPlanTo.getDepartmentId());
                carPlan.setWorkTypeId(carPlanTo.getWorkTypeId());
                carPlan.setWorkGroupId(carPlanTo.getWorkGroupId());
                carPlan.setWorkRuleId(carPlanTo.getWorkRuleId());
                carPlan.setWorkCycle(carPlanTo.getWorkCycle());
                carPlan.setWorkTrip(carPlanTo.getWorkTrip());
                carPlan.setExpectOilConsumption(carPlanTo.getExpectOilConsumption());
                carPlan.setExpectWaterConsumption(carPlanTo.getExpectWaterConsumption());
                carPlan.setExpectTimeConsumption(carPlanTo.getExpectTimeConsumption());
                carPlan.setExpectMileage(carPlanTo.getExpectMileage());
                carPlan.setWorkStartDate(carPlanTo.getWorkStartDate());
                carPlan.setWorkEndDate(carPlanTo.getWorkEndDate());
                carPlan.setIsEnable(carPlanTo.getIsEnable());
                carPlan.setRemark(carPlanTo.getRemark());
                carPlan.setWorkType(carPlanTo.getWorkType());
                carPlan.setType(carPlanTo.getType());
                carPlanMapper.insert(carPlan);

                Set<String> carIds = carPlanTo.getCarIdList();
                for (String carId : carIds) {
                    CarPlanCar carPlanCar = new CarPlanCar();
                    carPlanCar.setCarPlanId(carPlan.getId());
                    carPlanCar.setCarId(carId);
                    carPlanCarMapper.insert(carPlanCar);
                }
                if (null != carPlanTo.getCarPlanCollectStandIdList() && !carPlanTo.getCarPlanCollectStandIdList().isEmpty()) {
                    Set<String> collectStandList = carPlanTo.getCarPlanCollectStandIdList();
                    for (String collectStandId : collectStandList) {
                        CarPlanCollectStand carPlanCollectStand = new CarPlanCollectStand();
                        carPlanCollectStand.setCarPlanId(carPlan.getId());
                        carPlanCollectStand.setCollectStandId(collectStandId);
                        carPlanCollectStandMapper.insert(carPlanCollectStand);
                    }
                }

                if (null != carPlanTo.getCarPlanProcessStandIdList() && !carPlanTo.getCarPlanProcessStandIdList().isEmpty()) {
                    Set<String> processStandList = carPlanTo.getCarPlanProcessStandIdList();
                    for (String processStandId : processStandList) {
                        CarPlanProcessStand carPlanProcessStand = new CarPlanProcessStand();
                        carPlanProcessStand.setCarPlanId(carPlan.getId());
                        carPlanProcessStand.setProcessStandId(processStandId);
                        carPlanProcessStandMapper.insert(carPlanProcessStand);
                    }
                }
            }

        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        CarPlanMessageVo carPlanMessageVo = new CarPlanMessageVo();
        carPlanMessageVo.setMessage(message);
        return carPlanMessageVo;
    }

    @Override
    public List<CarPlan> repeatPlanName(CarPlanTo carPlanTo) {
        List<Grid> gridListOne = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridListOne.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<String> planNameList = new ArrayList<>();
        List<String> gridIdList = carPlanTo.getGridIdList();
        for (String gridId : gridIdList) {
            if (carPlanTo.getModule() == 1) {
                Grid grid1 = gridMap.get(gridId);
                if (null != grid1) {
                    planNameList.add(carPlanTo.getPlanName() + "-" + grid1.getName());
                }
            } else {
                planNameList.add(carPlanTo.getPlanName());
            }
        }
        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
        carPlanQueryWrapper.lambda().in(CarPlan::getPlanName, planNameList);
        List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
        return carPlanList;
    }

    @Override
    @Transactional
    public CarPlanMessageVo update(CarPlanTo carPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<String> carPlanIdList = carPlanTo.getCarPlanIdList();
            for (String carPlanId : carPlanIdList) {
                if (carPlanTo.getFlag() == 0) {
                    Set<String> carIds = carPlanTo.getCarIdList();
                    if (null != carIds && !carIds.isEmpty()) {
                        for (String carId : carIds) {
                            CarPlanCar carPlanCar = new CarPlanCar();
                            carPlanCar.setCarPlanId(carPlanId);
                            carPlanCar.setCarId(carId);
                            carPlanCarMapper.insert(carPlanCar);
                        }
                    }

                    if (null != carPlanTo.getCarPlanCollectPointIdList() && !carPlanTo.getCarPlanCollectPointIdList().isEmpty()) {

                        Set<String> collectPointList = carPlanTo.getCarPlanCollectPointIdList();
                        for (String collectPointId : collectPointList) {
                            CarPlanCollectPoint carPlanCollectPoint = new CarPlanCollectPoint();
                            carPlanCollectPoint.setCarPlanId(carPlanId);
                            carPlanCollectPoint.setCollectPointId(collectPointId);
                            carPlanCollectPointMapper.insert(carPlanCollectPoint);
                        }
                    }

                    if (null != carPlanTo.getCarPlanCollectStandIdList() && !carPlanTo.getCarPlanCollectStandIdList().isEmpty()) {
                        Set<String> collectStandList = carPlanTo.getCarPlanCollectStandIdList();
                        for (String collectStandId : collectStandList) {
                            CarPlanCollectStand carPlanCollectStand = new CarPlanCollectStand();
                            carPlanCollectStand.setCarPlanId(carPlanId);
                            carPlanCollectStand.setCollectStandId(collectStandId);
                            carPlanCollectStandMapper.insert(carPlanCollectStand);
                        }
                    }
                    if (null != carPlanTo.getCarPlanProcessStandIdList() && !carPlanTo.getCarPlanProcessStandIdList().isEmpty()) {
                        Set<String> processStandList = carPlanTo.getCarPlanProcessStandIdList();
                        for (String processStandId : processStandList) {
                            CarPlanProcessStand carPlanProcessStand = new CarPlanProcessStand();
                            carPlanProcessStand.setCarPlanId(carPlanId);
                            carPlanProcessStand.setProcessStandId(processStandId);
                            carPlanProcessStandMapper.insert(carPlanProcessStand);
                        }
                    }

                } else if (carPlanTo.getFlag() == 1) {
                    if (null != carPlanTo.getCarIdList() && !carPlanTo.getCarIdList().isEmpty()) {
                        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
                        carPlanCarQueryWrapper.lambda().eq(CarPlanCar::getCarPlanId, carPlanId);
                        carPlanCarQueryWrapper.lambda().in(CarPlanCar::getCarId, carPlanTo.getCarIdList());
                        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
                        List<String> carPlanCarIdList = new ArrayList<>();
                        for (CarPlanCar carPlanCar : carPlanCarList) {
                            carPlanCarIdList.add(carPlanCar.getId());
                        }
                        if (!carPlanCarIdList.isEmpty()) {
                            carPlanCarMapper.deleteBatchIds(carPlanCarIdList);
                        }
                    }
                    if (null != carPlanTo.getCarPlanCollectPointIdList() && !carPlanTo.getCarPlanCollectPointIdList().isEmpty()) {
                        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
                        carPlanCollectPointQueryWrapper.lambda().eq(CarPlanCollectPoint::getCarPlanId, carPlanId);
                        carPlanCollectPointQueryWrapper.lambda().in(CarPlanCollectPoint::getCollectPointId, carPlanTo.getCarPlanCollectPointIdList());
                        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
                        List<String> carPlanCollectPointIdList = new ArrayList<>();
                        for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList) {
                            carPlanCollectPointIdList.add(carPlanCollectPoint.getId());
                        }
                        if (!carPlanCollectPointIdList.isEmpty()) {
                            carPlanCollectPointMapper.deleteBatchIds(carPlanCollectPointIdList);
                        }
                    }

                    if (null != carPlanTo.getCarPlanCollectStandIdList() && !carPlanTo.getCarPlanCollectStandIdList().isEmpty()) {
                        QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
                        carPlanCollectStandQueryWrapper.lambda().eq(CarPlanCollectStand::getCarPlanId, carPlanId);
                        carPlanCollectStandQueryWrapper.lambda().in(CarPlanCollectStand::getCollectStandId, carPlanTo.getCarPlanCollectStandIdList());
                        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
                        List<String> carPlanCollectStandIdList = new ArrayList<>();
                        for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList) {
                            carPlanCollectStandIdList.add(carPlanCollectStand.getId());
                        }
                        if (!carPlanCollectStandIdList.isEmpty()) {
                            carPlanCollectStandMapper.deleteBatchIds(carPlanCollectStandIdList);
                        }
                    }
                    if (null != carPlanTo.getCarPlanProcessStandIdList() && !carPlanTo.getCarPlanProcessStandIdList().isEmpty()) {

                        QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
                        carPlanProcessStandQueryWrapper.lambda().eq(CarPlanProcessStand::getCarPlanId, carPlanId);
                        carPlanProcessStandQueryWrapper.lambda().in(CarPlanProcessStand::getProcessStandId, carPlanTo.getCarPlanProcessStandIdList());
                        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
                        List<String> carPlanProcessStandIdList = new ArrayList<>();
                        for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList) {
                            carPlanProcessStandIdList.add(carPlanProcessStand.getId());
                        }
                        if (!carPlanProcessStandIdList.isEmpty()) {
                            carPlanProcessStandMapper.deleteBatchIds(carPlanProcessStandIdList);
                        }
                    }
                } else {
                    QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                    carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
                    carPlanQueryWrapper.lambda().eq(CarPlan::getId, carPlanId);
                    CarPlan carPlan = carPlanMapper.selectOne(carPlanQueryWrapper);
                    carPlan.setUpdateTime(new Date());
                    carPlan.setPlanName(carPlanTo.getPlanName());
                    carPlan.setGridId(carPlanTo.getGridId());
                    carPlan.setDepartmentId(carPlanTo.getDepartmentId());
                    carPlan.setWorkTypeId(carPlanTo.getWorkTypeId());
                    carPlan.setWorkGroupId(carPlanTo.getWorkGroupId());
                    carPlan.setWorkRuleId(carPlanTo.getWorkRuleId());
                    carPlan.setWorkCycle(carPlanTo.getWorkCycle());
                    carPlan.setWorkTrip(carPlanTo.getWorkTrip());
                    carPlan.setExpectOilConsumption(carPlanTo.getExpectOilConsumption());
                    carPlan.setExpectWaterConsumption(carPlanTo.getExpectWaterConsumption());
                    carPlan.setExpectTimeConsumption(carPlanTo.getExpectTimeConsumption());
                    carPlan.setWorkEndDate(carPlanTo.getWorkEndDate());
                    carPlan.setIsEnable(carPlanTo.getIsEnable());
                    carPlan.setRemark(carPlanTo.getRemark());

                    carPlanMapper.updateById(carPlan);

                    Set<String> carIds = carPlanTo.getCarIdList();
                    if (null != carIds && !carIds.isEmpty()) {
                        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
                        carPlanCarQueryWrapper.lambda().eq(CarPlanCar::getCarPlanId, carPlanId);
                        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
                        List<String> carPlanCarIdList = new ArrayList<>();
                        for (CarPlanCar carPlanCar : carPlanCarList) {
                            carPlanCarIdList.add(carPlanCar.getId());
                        }
                        if (!carPlanCarIdList.isEmpty()) {
                            carPlanCarMapper.deleteBatchIds(carPlanCarIdList);
                        }
                        for (String carId : carIds) {
                            CarPlanCar carPlanCar = new CarPlanCar();
                            carPlanCar.setCarPlanId(carPlan.getId());
                            carPlanCar.setCarId(carId);
                            carPlanCarMapper.insert(carPlanCar);
                        }
                    }
                    if (null != carPlanTo.getCarPlanCollectPointIdList() && !carPlanTo.getCarPlanCollectPointIdList().isEmpty()) {
                        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
                        carPlanCollectPointQueryWrapper.lambda().eq(CarPlanCollectPoint::getCarPlanId, carPlanId);
                        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
                        List<String> carPlanCollectPointIdList = new ArrayList<>();
                        for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList) {
                            carPlanCollectPointIdList.add(carPlanCollectPoint.getId());
                        }
                        if (!carPlanCollectPointIdList.isEmpty()) {
                            carPlanCollectPointMapper.deleteBatchIds(carPlanCollectPointIdList);
                        }
                        Set<String> collectPointList = carPlanTo.getCarPlanCollectPointIdList();
                        for (String collectPointId : collectPointList) {
                            CarPlanCollectPoint carPlanCollectPoint = new CarPlanCollectPoint();
                            carPlanCollectPoint.setCarPlanId(carPlan.getId());
                            carPlanCollectPoint.setCollectPointId(collectPointId);
                            carPlanCollectPointMapper.insert(carPlanCollectPoint);
                        }
                    }

                    if (null != carPlanTo.getCarPlanCollectStandIdList() && !carPlanTo.getCarPlanCollectStandIdList().isEmpty()) {
                        QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
                        carPlanCollectStandQueryWrapper.lambda().eq(CarPlanCollectStand::getCarPlanId, carPlanId);
                        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
                        List<String> carPlanCollectStandIdList = new ArrayList<>();
                        for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList) {
                            carPlanCollectStandIdList.add(carPlanCollectStand.getId());
                        }
                        if (!carPlanCollectStandIdList.isEmpty()) {
                            carPlanCollectStandMapper.deleteBatchIds(carPlanCollectStandIdList);
                        }
                        Set<String> collectStandList = carPlanTo.getCarPlanCollectStandIdList();
                        for (String collectStandId : collectStandList) {
                            CarPlanCollectStand carPlanCollectStand = new CarPlanCollectStand();
                            carPlanCollectStand.setCarPlanId(carPlan.getId());
                            carPlanCollectStand.setCollectStandId(collectStandId);
                            carPlanCollectStandMapper.insert(carPlanCollectStand);
                        }
                    }

                    if (null != carPlanTo.getCarPlanProcessStandIdList() && !carPlanTo.getCarPlanProcessStandIdList().isEmpty()) {

                        QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
                        carPlanProcessStandQueryWrapper.lambda().eq(CarPlanProcessStand::getCarPlanId, carPlanId);
                        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
                        List<String> carPlanProcessStandIdList = new ArrayList<>();
                        for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList) {
                            carPlanProcessStandIdList.add(carPlanProcessStand.getId());
                        }
                        if (!carPlanProcessStandIdList.isEmpty()) {
                            carPlanProcessStandMapper.deleteBatchIds(carPlanProcessStandIdList);
                        }
                        Set<String> processStandList = carPlanTo.getCarPlanProcessStandIdList();
                        for (String processStandId : processStandList) {
                            CarPlanProcessStand carPlanProcessStand = new CarPlanProcessStand();
                            carPlanProcessStand.setCarPlanId(carPlan.getId());
                            carPlanProcessStand.setProcessStandId(processStandId);
                            carPlanProcessStandMapper.insert(carPlanProcessStand);
                        }
                    }

                }
            }

        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        CarPlanMessageVo carPlanMessageVo = new CarPlanMessageVo();
        carPlanMessageVo.setMessage(message);
        return carPlanMessageVo;
    }

    @Override
    public CarPlanVo read(CarPlanTo carPlanTo) {

        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getName()).collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));


        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
        carPlanQueryWrapper.lambda().eq(CarPlan::getId, carPlanTo.getCarPlanId());
        CarPlan carPlan = carPlanMapper.selectOne(carPlanQueryWrapper);

        CarPlanVo carPlanVo = new CarPlanVo();
        carPlanVo.setId(carPlan.getId());
        carPlanVo.setPlanName(carPlan.getPlanName());
        carPlanVo.setWorkCycle(carPlan.getWorkCycle());
        carPlanVo.setWorkTrip(carPlan.getWorkTrip());
        carPlanVo.setWorkType(carPlan.getWorkType());
        carPlanVo.setExpectOilConsumption(carPlan.getExpectOilConsumption());
        carPlanVo.setExpectWaterConsumption(carPlan.getExpectWaterConsumption());
        carPlanVo.setExpectTimeConsumption(carPlan.getExpectTimeConsumption());
        carPlanVo.setWorkStartDate(carPlan.getWorkStartDate());
        carPlanVo.setWorkEndDate(carPlan.getWorkEndDate());
        carPlanVo.setRemark(carPlan.getRemark());
        carPlanVo.setExpectMileage(carPlan.getExpectMileage());
        Grid grid = gridMap.get(carPlan.getGridId());
        if (null != grid) {
            carPlanVo.setGridId(grid.getGridId());
            carPlanVo.setGridName(grid.getName());
        }
        SysDepart sysDepart = sysDepartMap.get(carPlan.getDepartmentId());
        if (null != sysDepart) {
            carPlanVo.setDepartmentId(sysDepart.getId());
            carPlanVo.setDepartmentName(sysDepart.getDepartName());
        }
        TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
        if (null != tlOperation) {
            carPlanVo.setWorkTypeId(tlOperation.getId());
            carPlanVo.setWorkTypeName(tlOperation.getOperationtype());
        }
        WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
        if (null != workGroup) {
            carPlanVo.setWorkGroupId(workGroup.getWorkGroupId());
            carPlanVo.setWorkGroupName(workGroup.getName());
        }
        WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
        if (null != workRule) {
            carPlanVo.setWorkRuleId(workRule.getWorkRuleId());
            carPlanVo.setWorkRuleName(workRule.getRuleName());
        }
        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
        carPlanCarQueryWrapper.lambda().eq(CarPlanCar::getCarPlanId, carPlanTo.getCarPlanId());
        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
        Set<String> carNumberList = new HashSet<>();
        Set<String> carIdList = new HashSet<>();
        for (CarPlanCar carPlanCar : carPlanCarList) {
            TlCar tlCar = carMap.get(carPlanCar.getCarId());
            if (null != tlCar) {
                carNumberList.add(tlCar.getCarnumber());
                carIdList.add(tlCar.getId());
            }
        }
        if (!carNumberList.isEmpty()) {
            carPlanVo.setCarNumberList(carNumberList);
            carPlanVo.setCarIdList(carIdList);
        }

        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
        carPlanCollectPointQueryWrapper.lambda().eq(CarPlanCollectPoint::getCarPlanId, carPlanTo.getCarPlanId());
        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
        Set<String> carPlanCollectPointNameList = new HashSet<>();
        Set<String> carPlanCollectPointIdList = new HashSet<>();
        for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList) {
            TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectPoint.getCollectPointId());
            if (null != tlFacilities) {
                carPlanCollectPointNameList.add(tlFacilities.getFaclname());
                carPlanCollectPointIdList.add(tlFacilities.getId());
            }
        }
        if (!carPlanCollectPointIdList.isEmpty()) {
            carPlanVo.setCarPlanCollectPointNameList(carPlanCollectPointNameList);
            carPlanVo.setCarPlanCollectPointIdList(carPlanCollectPointIdList);
        }


        QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
        carPlanCollectStandQueryWrapper.lambda().eq(CarPlanCollectStand::getCarPlanId, carPlanTo.getCarPlanId());
        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
        Set<String> carPlanCollectStandNameList = new HashSet<>();
        Set<String> carPlanCollectStandIdList = new HashSet<>();
        for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList) {
            TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectStand.getCollectStandId());
            if (null != tlFacilities) {
                carPlanCollectStandNameList.add(tlFacilities.getFaclname());
                carPlanCollectStandIdList.add(tlFacilities.getId());
            }
        }
        if (!carPlanCollectStandIdList.isEmpty()) {
            carPlanVo.setCarPlanCollectStandNameList(carPlanCollectStandNameList);
            carPlanVo.setCarPlanCollectStandIdList(carPlanCollectStandIdList);

        }

        QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
        carPlanProcessStandQueryWrapper.lambda().eq(CarPlanProcessStand::getCarPlanId, carPlanTo.getCarPlanId());
        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
        Set<String> carPlanProcessStandNameList = new HashSet<>();
        Set<String> carPlanProcessStandIdList = new HashSet<>();
        for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList) {
            TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanProcessStand.getProcessStandId());
            if (null != tlFacilities) {
                carPlanProcessStandNameList.add(tlFacilities.getFaclname());
                carPlanProcessStandIdList.add(tlFacilities.getId());
            }
        }
        if (!carPlanProcessStandNameList.isEmpty()) {
            carPlanVo.setCarPlanProcessStandIdList(carPlanProcessStandIdList);
            carPlanVo.setCarPlanProcessStandNameList(carPlanProcessStandNameList);
        }

        return carPlanVo;
    }

    @Override
    public CarPlanMessageVo updateIsEnable(CarPlanTo carPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<String> carPlanIdList = carPlanTo.getCarPlanIdList();
            for (String carPlanId : carPlanIdList) {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
                carPlanQueryWrapper.lambda().eq(CarPlan::getId, carPlanId);
                CarPlan carPlan = carPlanMapper.selectOne(carPlanQueryWrapper);
                carPlan.setIsEnable(carPlanTo.getIsEnable());
                carPlanMapper.updateById(carPlan);
            }

        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        CarPlanMessageVo carPlanMessageVo = new CarPlanMessageVo();
        carPlanMessageVo.setMessage(message);
        return carPlanMessageVo;
    }

    @Override
    @Transactional
    public CarPlanMessageVo delete(CarPlanTo carPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<String> carPlanIdList = carPlanTo.getCarPlanIdList();

            List<String> carPlanIdsList = new ArrayList<>();
            QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
            carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
            carPlanQueryWrapper.lambda().in(CarPlan::getId, carPlanIdList);
            List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
            for (CarPlan carPlan : carPlanList) {
                carPlanIdsList.add(carPlan.getId());
            }
            if (!carPlanIdsList.isEmpty()) {
                carPlanMapper.deleteBatchIds(carPlanIdsList);
            }
            QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
            carPlanCarQueryWrapper.lambda().in(CarPlanCar::getCarPlanId, carPlanIdList);
            List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
            List<String> carPlanCarIdList = new ArrayList<>();
            for (CarPlanCar carPlanCar : carPlanCarList) {
                carPlanCarIdList.add(carPlanCar.getId());
            }
            if (!carPlanCarIdList.isEmpty()) {
                carPlanCarMapper.deleteBatchIds(carPlanCarIdList);
            }

            QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
            carPlanCollectPointQueryWrapper.lambda().in(CarPlanCollectPoint::getCarPlanId, carPlanIdList);
            List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
            List<String> carPlanCollectPointIdList = new ArrayList<>();
            for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList) {
                carPlanCollectPointIdList.add(carPlanCollectPoint.getId());
            }
            if (!carPlanCollectPointIdList.isEmpty()) {
                carPlanCollectPointMapper.deleteBatchIds(carPlanCollectPointIdList);
            }


            QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
            carPlanCollectStandQueryWrapper.lambda().in(CarPlanCollectStand::getCarPlanId, carPlanIdList);
            List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
            List<String> carPlanCollectStandIdList = new ArrayList<>();
            for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList) {
                carPlanCollectStandIdList.add(carPlanCollectStand.getId());
            }
            if (!carPlanCollectStandIdList.isEmpty()) {
                carPlanCollectStandMapper.deleteBatchIds(carPlanCollectStandIdList);
            }

            QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
            carPlanProcessStandQueryWrapper.lambda().in(CarPlanProcessStand::getCarPlanId, carPlanIdList);
            List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
            List<String> carPlanProcessStandIdList = new ArrayList<>();
            for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList) {
                carPlanProcessStandIdList.add(carPlanProcessStand.getId());
            }
            if (!carPlanProcessStandIdList.isEmpty()) {
                carPlanProcessStandMapper.deleteBatchIds(carPlanProcessStandIdList);
            }
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        CarPlanMessageVo carPlanMessageVo = new CarPlanMessageVo();
        carPlanMessageVo.setMessage(message);
        return carPlanMessageVo;
    }

    @Override
    public WorkGroupByCarPlanVo deleteWorkGroup(CarPlanTo carPlanTo) {
        WorkGroupByCarPlanVo workGroupByCarPlanVo = new WorkGroupByCarPlanVo();
        List<String> carPlanIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(carPlanTo.getModule())) {
            if (carPlanTo.getModule() == 3) {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 1);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkGroupId, carPlanTo.getWorkGroupIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workGroupId : carPlanTo.getWorkGroupIdList()) {
                        workGroupMapper.deleteById(workGroupId);
                    }
                }
            } else if (carPlanTo.getModule() == 4) {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 2);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkGroupId, carPlanTo.getWorkGroupIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workGroupId : carPlanTo.getWorkGroupIdList()) {
                        workGroupMapper.deleteById(workGroupId);
                    }
                }
            } else {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 3);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkGroupId, carPlanTo.getWorkGroupIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workGroupId : carPlanTo.getWorkGroupIdList()) {
                        workGroupMapper.deleteById(workGroupId);
                    }
                }
            }
        }
        workGroupByCarPlanVo.setWorkPlanIdList(carPlanIds);
        return workGroupByCarPlanVo;
    }

    @Override
    public WorkGroupByCarPlanVo deleteWorkRule(CarPlanTo carPlanTo) {
        WorkGroupByCarPlanVo workGroupByCarPlanVo = new WorkGroupByCarPlanVo();
        List<String> carPlanIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(carPlanTo.getModule())) {
            if (carPlanTo.getModule() == 3) {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 1);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkRuleId, carPlanTo.getWorkRuleIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workRuleId : carPlanTo.getWorkRuleIdList()) {
                        workRuleMapper.deleteById(workRuleId);
                    }
                }
            } else if (carPlanTo.getModule() == 4) {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 2);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkRuleId, carPlanTo.getWorkRuleIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workRuleId : carPlanTo.getWorkRuleIdList()) {
                        workRuleMapper.deleteById(workRuleId);
                    }
                }
            } else {
                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 3);
                carPlanQueryWrapper.lambda().in(CarPlan::getWorkRuleId, carPlanTo.getWorkRuleIdList());
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                if (!carPlanList.isEmpty()) {
                    for (CarPlan carPlan : carPlanList) {
                        carPlanIds.add(carPlan.getId());
                    }

                } else {
                    for (String workRuleId : carPlanTo.getWorkRuleIdList()) {
                        workRuleMapper.deleteById(workRuleId);
                    }
                }
            }
        }
        workGroupByCarPlanVo.setWorkPlanIdList(carPlanIds);
        return workGroupByCarPlanVo;
    }

    @Override
    public List<CarPlan> excelExport(CarPlanTo carPlanTo) {
        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getGridId()).collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));


        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
        Map<String, List<CarPlanCollectPoint>> carPlanCollectPointMap = carPlanCollectPointList.stream().collect(Collectors.groupingBy(CarPlanCollectPoint::getCarPlanId));

        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
        Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap = carPlanCollectStandList.stream().collect(Collectors.groupingBy(CarPlanCollectStand::getCarPlanId));

        QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
        Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap = carPlanProcessStandList.stream().collect(Collectors.groupingBy(CarPlanProcessStand::getCarPlanId));


        QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
        carPlanQueryWrapper.lambda().eq(CarPlan::getType, carPlanTo.getType());
        if (StringUtils.isNotEmpty(carPlanTo.getWorkTypeId())) {
            carPlanQueryWrapper.lambda().in(CarPlan::getWorkTypeId, carPlanTo.getWorkTypeId());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getPlanName())) {
            carPlanQueryWrapper.lambda().like(CarPlan::getPlanName, carPlanTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carPlanTo.getDepartmentId())) {
            List<String> departmentList = departService.getChildIdsByIId(carPlanTo.getDepartmentId());
            carPlanQueryWrapper.lambda().in(CarPlan::getDepartmentId, departmentList);
        }
        if (null != carPlanTo.getIds() && !carPlanTo.getIds().isEmpty()) {
            carPlanQueryWrapper.lambda().in(CarPlan::getId, carPlanTo.getIds());
        }
        QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
        List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
        Map<String, List<CarPlanCar>> carPlanCarMap = carPlanCarList.stream().collect(Collectors.groupingBy(CarPlanCar::getCarPlanId));

        List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);

        for (CarPlan carPlan : carPlanList) {
            List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
            if (null != carPlanCarList1) {
                Set<String> carIdsList = new HashSet<>();
                Set<String> carNameList = new HashSet<>();

                for (CarPlanCar carPlanCar : carPlanCarList1) {
                    TlCar tlCar = carMap.get(carPlanCar.getCarId());
                    if (null != tlCar) {
                        carIdsList.add(tlCar.getId());
                        carNameList.add(tlCar.getCarnumber());
                    }
                }
                carPlan.setCarIds(String.join(",", carIdsList));
                carPlan.setCarNumbers(String.join(",", carNameList));

            }
            Grid grid = gridMap.get(carPlan.getGridId());
            if (null != grid) {
                carPlan.setGridName(grid.getName());
            }
            WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
            if (null != workGroup) {
                carPlan.setWorkGroupName(workGroup.getName());
            }
            TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
            if (null != tlOperation) {
                carPlan.setWorkTypeName(tlOperation.getOperationtype());
            }

            List<CarPlanCollectPoint> carPlanCollectPointList1 = carPlanCollectPointMap.get(carPlan.getId());
            List<String> facNameList = new ArrayList<>();
            if (null != carPlanCollectPointList1) {
                for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectPoint.getCollectPointId());
                    if (null != tlFacilities) {
                        facNameList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setCollectPointNames(String.join(",", facNameList));

            List<CarPlanCollectStand> carPlanCollectStandList1 = carPlanCollectStandMap.get(carPlan.getId());
            Set<String> collectStandList = new HashSet<>();
            if (null != carPlanCollectStandList1) {
                for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectStand.getCollectStandId());
                    if (null != tlFacilities) {
                        collectStandList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setCollectStandNames(String.join(",", collectStandList));

            List<CarPlanProcessStand> carPlanProcessStandList1 = carPlanProcessStandMap.get(carPlan.getId());
            Set<String> processStandList = new HashSet<>();
            if (null != carPlanProcessStandList1) {
                for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList1) {
                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanProcessStand.getProcessStandId());
                    if (null != tlFacilities) {
                        processStandList.add(tlFacilities.getFaclname());
                    }
                }
            }
            carPlan.setProcessStandNames(String.join(",", processStandList));

        }

        if (null != carPlanTo.getCarIds() && !carPlanTo.getCarIds().isEmpty()) {
            String carIds = String.join(",", carPlanTo.getCarIds());
            List<CarPlan> carPlanListFilterCarNumber = carPlanList.stream().filter(carPlan -> null != carPlan.getCarIds() && carPlan.getCarIds().contains(carIds)).collect(toList());
            return carPlanListFilterCarNumber;
        } else {
            return carPlanList;
        }

    }


    @Override
    public void carPlanExcelImport(List<CarPlan> carPlanList, Integer type) throws ExcelReaderDataException {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().filter(grid -> null != grid.getName()).collect(Collectors.groupingBy(Grid::getName, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        List<TlCar> tlCarList = tlCarMapper.selectList(new QueryWrapper<>());
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getCarnumber, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getName, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().filter(tlFacilities -> null != tlFacilities.getFaclname()).collect(Collectors.groupingBy(TlFacilities::getFaclname, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        List<String> carIds = new ArrayList<>();
        List<String> collectPointIds = new ArrayList<>();
        List<String> collectStandIds = new ArrayList<>();
        List<String> processStandIds = new ArrayList<>();

        int num = 1;
        String str = "";
        for (CarPlan carPlan : carPlanList) {
            if (type == 1) {
                carPlan.setId(StringUtils.uuid());
                carPlan.setIsEnable(1);
                if (null != gridMap.get(carPlan.getGridName())) {
                    carPlan.setGridId(gridMap.get(carPlan.getGridName()).getGridId());
                } else {
                    str += "第" + num + "条的“‘网格名称’:‘" + carPlan.getGridName() + "’”" + "不存在;##";
                }
                String[] split = carPlan.getCarNumbers().split(",");
                for (String carNumber : split) {
                    if (null != carMap.get(carNumber)) {
                        carIds.add(carMap.get(carNumber).getId());
                        CarPlanCar carPlanCar = new CarPlanCar();
                        carPlanCar.setId(StringUtils.uuid());
                        carPlanCar.setCarPlanId(carPlan.getId());
                        carPlanCar.setCarId(carMap.get(carNumber).getId());
                        carPlanCarMapper.insert(carPlanCar);
                    } else {
                        str += "第" + num + "条的“‘车牌号’:‘" + carNumber + "’”" + "不存在;##";
                    }

                }
                if (null != workGroupMap.get(carPlan.getWorkGroupName())) {
                    carPlan.setWorkGroupId(workGroupMap.get(carPlan.getWorkGroupName()).getWorkGroupId());
                } else {
                    str += "第" + num + "条的“‘班组名称’:‘" + carPlan.getWorkGroupName() + "’”" + "不存在;##";
                }
                num++;

            } else if (type == 2) {

                carPlan.setId(StringUtils.uuid());
                carPlan.setIsEnable(1);
                if (null != gridMap.get(carPlan.getGridName())) {
                    carPlan.setGridId(gridMap.get(carPlan.getGridName()).getGridId());
                } else {
                    str += "第" + num + "条的“‘网格名称’:‘" + carPlan.getGridName() + "’”" + "不存在;##";
                }
                String[] split = carPlan.getCollectPointNames().split(",");
                for (String pointName : split) {
                    if (null != tlFacilitiesMap.get(pointName)) {
                        collectPointIds.add(tlFacilitiesMap.get(pointName).getId());
                        CarPlanCollectPoint carPlanCollectPoint = new CarPlanCollectPoint();
                        carPlanCollectPoint.setId(StringUtils.uuid());
                        carPlanCollectPoint.setCarPlanId(carPlan.getId());
                        carPlanCollectPoint.setCollectPointId(tlFacilitiesMap.get(pointName).getId());
                        carPlanCollectPointMapper.insert(carPlanCollectPoint);
                    } else {
                        str += "第" + num + "条的“‘收集点’:‘" + pointName + "’”" + "不存在;##";
                    }

                }
                if (null != workGroupMap.get(carPlan.getWorkGroupName())) {
                    carPlan.setWorkGroupId(workGroupMap.get(carPlan.getWorkGroupName()).getWorkGroupId());
                } else {
                    str += "第" + num + "条的“‘班组名称’:‘" + carPlan.getWorkGroupName() + "’”" + "不存在;##";
                }
                num++;

            } else {
                carPlan.setId(StringUtils.uuid());
                carPlan.setIsEnable(1);
                if (null != gridMap.get(carPlan.getGridName())) {
                    carPlan.setGridId(gridMap.get(carPlan.getGridName()).getGridId());
                } else {
                    str += "第" + num + "条的“‘网格名称’:‘" + carPlan.getGridName() + "’”" + "不存在;##";
                }
                String[] split = carPlan.getCollectStandNames().split(",");
                for (String standName : split) {
                    if (null != tlFacilitiesMap.get(standName)) {
                        collectStandIds.add(tlFacilitiesMap.get(standName).getId());
                        CarPlanCollectStand carPlanCollectStand = new CarPlanCollectStand();
                        carPlanCollectStand.setId(StringUtils.uuid());
                        carPlanCollectStand.setCarPlanId(carPlan.getId());
                        carPlanCollectStand.setCollectStandId(tlFacilitiesMap.get(standName).getId());
                        carPlanCollectStandMapper.insert(carPlanCollectStand);
                    } else {
                        str += "第" + num + "条的“‘中转/收集站名称’:‘" + standName + "’”" + "不存在;##";
                    }
                }
                String[] split1 = carPlan.getProcessStandNames().split(",");
                for (String processStandName : split1) {
                    if (null != tlFacilitiesMap.get(processStandName)) {
                        processStandIds.add(tlFacilitiesMap.get(processStandName).getId());
                        CarPlanProcessStand carPlanProcessStand = new CarPlanProcessStand();
                        carPlanProcessStand.setId(StringUtils.uuid());
                        carPlanProcessStand.setCarPlanId(carPlan.getId());
                        carPlanProcessStand.setProcessStandId(tlFacilitiesMap.get(processStandName).getId());
                        carPlanProcessStandMapper.insert(carPlanProcessStand);
                    } else {
                        str += "第" + num + "条的“‘处理站名称’:‘" + processStandName + "’”" + "不存在;##";
                    }
                }
                if (null != workGroupMap.get(carPlan.getWorkGroupName())) {
                    carPlan.setWorkGroupId(workGroupMap.get(carPlan.getWorkGroupName()).getWorkGroupId());
                } else {
                    str += "第" + num + "条的“‘班组名称’:‘" + carPlan.getWorkGroupName() + "’”" + "不存在;##";
                }
                num++;
            }
        }
        if (StringUtils.isNotEmpty(str)) {
            throw new ExcelReaderDataException("您导入的数据:##" + str);
        }

    }

    @Override
    @DataScope(deptAlias = "department_id", userAlias = "")
    public List<JxqsWorkPlanCountVo> mechanicalWorkPlanCount(BaseEntity entity, CarWorkPlanCountTo carWorkPlanCountTo) throws HssException {

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Jobset jobset = jobsetMapper.selectById(3);

        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
        Map<String, TlCar> carMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        if (null != carWorkPlanCountTo.getPlanIds() && !carWorkPlanCountTo.getPlanIds().isEmpty()) {
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanIds());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarNum, carWorkPlanCountTo.getCarNumber());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            QueryWrapper<RptCarWork> rptCarWorkQueryWrapper1 = new QueryWrapper<>();
            rptCarWorkQueryWrapper1.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
            List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper1);
            List<String> idList = new ArrayList<>();
            for (RptCarWork rptCarWork : rptCarWorkList) {
                idList.add(rptCarWork.getPlanId());
            }
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getPlanId, idList);
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            rptCarWorkQueryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));
        List<JxqsWorkPlanCountVo> jxqsWorkPlanCountVoList = new ArrayList<>();

        QueryWrapper<GridSnapshot> gridSnapshotQueryWrapper = new QueryWrapper<>();
        gridSnapshotQueryWrapper.lambda().between(GridSnapshot::getDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<GridSnapshot> gridSnapshotList = gridSnapshotMapper.selectList(gridSnapshotQueryWrapper);
        Map<String, List<GridSnapshot>> gridSnapshotMap = gridSnapshotList.stream().collect(Collectors.groupingBy(GridSnapshot::getGridId));

        for (String planId : rptCarWorkMap.keySet()) {
            Set<String> planNameList = new HashSet<>();
            Set<String> deptNameList = new HashSet<>();
            Set<String> completeCars = new HashSet<>();
            Set<String> completeCarNumbers = new HashSet<>();
            Set<String> gridNameList = new HashSet<>();
            Set<String> gridIdList = new HashSet<>();
            Set<String> shiftCarNumbers = new HashSet<>();
            JxqsWorkPlanCountVo jxqsWorkPlanCountVo = new JxqsWorkPlanCountVo();

            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(planId);


            jxqsWorkPlanCountVo.setWaterConsumption(BigDecimal.ZERO);
            jxqsWorkPlanCountVo.setEmptyMileage(BigDecimal.ZERO);
            jxqsWorkPlanCountVo.setWorkMileage(BigDecimal.ZERO);
            jxqsWorkPlanCountVo.setCompleteDays(0);
            BigDecimal workOil = BigDecimal.ZERO;
            BigDecimal onGuardOil = BigDecimal.ZERO;
            BigDecimal onGuardWorkTime = BigDecimal.ZERO;
            BigDecimal workTime = BigDecimal.ZERO;
            BigDecimal planningMileage = BigDecimal.ZERO;
            BigDecimal onGuardMileage = BigDecimal.ZERO;
            BigDecimal workMileage = BigDecimal.ZERO;
            if (null != rptCarWorkList1) {
                Integer completeDays = 0;

                Map<String, List<RptCarWork>> rptCarWorkGroupByWorkDate1 = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                jxqsWorkPlanCountVo.setPlanDays(rptCarWorkGroupByWorkDate1.keySet().size());
                for (String workDate : rptCarWorkGroupByWorkDate1.keySet()) {
                    Boolean isFinish = true;
                    List<RptCarWork> rptCarWorkList3 = rptCarWorkGroupByWorkDate1.get(workDate);
                    if (null != rptCarWorkList3) {
                        Map<String, List<RptCarWork>> rptCarWorkByShiftId = rptCarWorkList3.stream().collect(Collectors.groupingBy(RptCarWork::getShiftId));
                        for (String shiftId : rptCarWorkByShiftId.keySet()) {
                            Integer tangCi = 0;
                            Integer workTangCi = 0;
                            Integer allProgress = 0;
                            List<RptCarWork> rptCarWorkList4 = rptCarWorkByShiftId.get(shiftId);
                            if (null != rptCarWorkList4) {
                                Map<String, List<RptCarWork>> rptCarWorkByCarId = rptCarWorkList4.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));
                                for (String carId : rptCarWorkByCarId.keySet()) {
                                    List<RptCarWork> rptCarWorkList2 = rptCarWorkByCarId.get(carId);
                                    for (RptCarWork rptCarWork : rptCarWorkList2) {

                                        allProgress = rptCarWork.getAllProgress();

                                        tangCi = rptCarWork.getTangCi();

                                        BigDecimal singleProgress = (rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()).divide(new BigDecimal(rptCarWork.getTangCi()), BigDecimal.ROUND_HALF_UP);

                                        BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);

                                        int thisTangCi = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();

                                        workTangCi += thisTangCi;
                                    }
                                }
                                planningMileage = planningMileage.add(new BigDecimal(allProgress));

                                if (workTangCi < tangCi) {
                                    isFinish = false;
                                }
                            }
                        }
                        if (isFinish) {
                            completeDays++;
                        }
                    }


                }

                jxqsWorkPlanCountVo.setCompleteDays(completeDays);
                jxqsWorkPlanCountVo.setUnCompleteDays(jxqsWorkPlanCountVo.getPlanDays() - jxqsWorkPlanCountVo.getCompleteDays());


                Map<String, List<RptCarWork>> rptCarWorkGroupByCarId = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));
                for (String carId : rptCarWorkGroupByCarId.keySet()) {
                    List<RptCarWork> rptCarWorkList2 = rptCarWorkGroupByCarId.get(carId);

                    if (null != rptCarWorkList2) {
                        Map<String, List<RptCarWork>> rptCarWorkGroupByWorkDate = rptCarWorkList2.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                        for (String workDate : rptCarWorkGroupByWorkDate.keySet()) {

                            List<RptCarWork> rptCarWorkList3 = rptCarWorkGroupByWorkDate.get(workDate);
                            if (null != rptCarWorkList3) {

                                for (RptCarWork rptCarWork : rptCarWorkList3) {

                                    if (null != rptCarWork.getWorkMileage() && rptCarWork.getWorkMileage().compareTo(BigDecimal.ZERO) > 0) {
                                        completeCars.add(carId);
                                        completeCarNumbers.add(null == carMap.get(carId) ? "" : carMap.get(carId).getCarnumber());
                                    }
                                }
                            }
                        }
                    }
                }
                //根据计划聚合的遍历
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    planNameList.add(rptCarWork.getPlanName());
                    jxqsWorkPlanCountVo.setPlanId(rptCarWork.getPlanId());

                    shiftCarNumbers.add(rptCarWork.getCarNum());
                    List<GridSnapshot> gridSnapshotList1 = gridSnapshotMap.get(rptCarWork.getGridId());
                    if (null != gridSnapshotList1) {
                        for (GridSnapshot gridSnapshot : gridSnapshotList1) {
                            gridNameList.add(gridSnapshot.getName());
                            gridIdList.add(gridSnapshot.getGridId());
                        }
                    }

                    workOil = workOil.add(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                    onGuardOil = onGuardOil.add(null == rptCarWork.getOnGuardOil() ? BigDecimal.ZERO : rptCarWork.getOnGuardOil());

                    onGuardWorkTime = onGuardWorkTime.add(null == rptCarWork.getOnGuardTime() ? BigDecimal.ZERO : rptCarWork.getOnGuardTime());
                    workTime = workTime.add(null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime());

                    jxqsWorkPlanCountVo.setPlanningMileage(planningMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));

                    workMileage = workMileage.add(null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage());
                    onGuardMileage = onGuardMileage.add(null == rptCarWork.getOnGuardMileage() ? BigDecimal.ZERO : rptCarWork.getOnGuardMileage());

                    SysDepart sysDepart = sysDepartMap.get(rptCarWork.getDepartmentId());
                    if (null != sysDepart) {
                        deptNameList.add(sysDepart.getDepartName());
                    }
                    jxqsWorkPlanCountVo.setWorkCycle(null == rptCarWork.getWorkCycle() ? 1 : rptCarWork.getWorkCycle());
                }
            }
            jxqsWorkPlanCountVo.setWorkOil(workOil);
            jxqsWorkPlanCountVo.setOnGuardOil(onGuardOil);
            jxqsWorkPlanCountVo.setEmptyOil(onGuardOil.subtract(workOil));

            jxqsWorkPlanCountVo.setWorkTime(Tools.secondToTime(workTime.longValue()));
            jxqsWorkPlanCountVo.setOnGuardWorkTime(Tools.secondToTime(onGuardWorkTime.longValue()));
            jxqsWorkPlanCountVo.setEmptyWorkTime(Tools.secondToTime((onGuardWorkTime.subtract(workTime)).longValue()));


            jxqsWorkPlanCountVo.setWorkMileage(workMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqsWorkPlanCountVo.setOnGuardMileage(onGuardMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqsWorkPlanCountVo.setEmptyMileage((onGuardMileage.subtract(workMileage)).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));

            jxqsWorkPlanCountVo.setCompeteCars(completeCars.size());
            jxqsWorkPlanCountVo.setCompleteCarNumbers(completeCarNumbers);
            jxqsWorkPlanCountVo.setShiftCarNumbers(shiftCarNumbers);
            jxqsWorkPlanCountVo.setDeptName(String.join(",", deptNameList));
            jxqsWorkPlanCountVo.setGridName(String.join(",", gridNameList));
            jxqsWorkPlanCountVo.setGridId(String.join(",", gridIdList));
            jxqsWorkPlanCountVo.setPlanName(String.join(",", planNameList));
            jxqsWorkPlanCountVo.setPlanNameList(planNameList);
            jxqsWorkPlanCountVoList.add(jxqsWorkPlanCountVo);
        }
        ListSortCount(jxqsWorkPlanCountVoList);
        return jxqsWorkPlanCountVoList;
    }


    @Override
    public List<JxqWorkPlanDetailVo> mechanicalWorkPlanDetail(CarWorkPlanCountTo carWorkPlanCountTo) throws ParseException {

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        if (null != carWorkPlanCountTo.getCarIds() && !carWorkPlanCountTo.getCarIds().isEmpty()) {
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getCarId, carWorkPlanCountTo.getCarIds());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            List<String> carNumbers = Arrays.asList(carWorkPlanCountTo.getCarNumber().split(","));
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getCarNum, carNumbers);
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getPlanName);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        Jobset jobset = jobsetMapper.selectById(3);

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId));

        QueryWrapper<GridSnapshot> gridSnapshotQueryWrapper = new QueryWrapper<>();
        gridSnapshotQueryWrapper.lambda().between(GridSnapshot::getDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<GridSnapshot> gridSnapshotList = gridSnapshotMapper.selectList(gridSnapshotQueryWrapper);
        Map<String, List<GridSnapshot>> gridSnapshotMap = gridSnapshotList.stream().collect(Collectors.groupingBy(GridSnapshot::getGridId));

        List<JxqWorkPlanDetailVo> jxqWorkPlanDetailVoList = new ArrayList<>();

        Map<String, List<RptCarWork>> rptCarWorkGroupByCarId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));

        for (String carId : rptCarWorkGroupByCarId.keySet()) {
            JxqWorkPlanDetailVo jxqWorkPlanDetailVo = new JxqWorkPlanDetailVo();

            BigDecimal nowProgress = BigDecimal.ZERO;
            BigDecimal onGuardMileage = BigDecimal.ZERO;
            BigDecimal workTime = BigDecimal.ZERO;
            BigDecimal onGuardWorkTime = BigDecimal.ZERO;
            BigDecimal planningMileage = BigDecimal.ZERO;
            jxqWorkPlanDetailVo.setStatus(3);
            List<RptCarWork> rptCarWorkList1 = rptCarWorkGroupByCarId.get(carId);
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    jxqWorkPlanDetailVo.setCarNumber(rptCarWork.getCarNum());
                    jxqWorkPlanDetailVo.setCarTypes(rptCarWork.getCarType());

                    SysDepart sysDepart = sysDepartMap.get(rptCarWork.getDepartmentId());
                    if (null != sysDepart) {
                        jxqWorkPlanDetailVo.setDeptName(sysDepart.getDepartName());
                    }
                    jxqWorkPlanDetailVo.setCarId(rptCarWork.getCarId());
                    jxqWorkPlanDetailVo.setWorkDate(rptCarWork.getWorkDate());
                    jxqWorkPlanDetailVo.setPlanId(rptCarWork.getPlanId());
                    jxqWorkPlanDetailVo.setPlanName(rptCarWork.getPlanName());
                    List<GridSnapshot> gridSnapshotList1 = gridSnapshotMap.get(rptCarWork.getGridId());
                    if (null != gridSnapshotList1) {
                        for (GridSnapshot gridSnapshot : gridSnapshotList1) {
                            jxqWorkPlanDetailVo.setGridId(gridSnapshot.getGridId());
                            jxqWorkPlanDetailVo.setGridName(gridSnapshot.getName());
                        }
                    }
                    jxqWorkPlanDetailVo.setWorkCycle(rptCarWork.getWorkCycle());
                    jxqWorkPlanDetailVo.setViolationsNumber((null == carViolationMap.get(rptCarWork.getCarId()) ? 0 : carViolationMap.get(rptCarWork.getCarId()).size()));//违规次数
                    jxqWorkPlanDetailVo.setAlarmNumber((null == alarmCarMap.get(rptCarWork.getCarId()) ? 0 : alarmCarMap.get(rptCarWork.getCarId()).size()));//报警次数
                    BigDecimal totalWorkOil = (null == jxqWorkPlanDetailVo.getWorkOil() ? BigDecimal.ZERO : jxqWorkPlanDetailVo.getWorkOil()).add(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                    BigDecimal totalOnGuardOil = (null == jxqWorkPlanDetailVo.getOnGuardOil() ? BigDecimal.ZERO : jxqWorkPlanDetailVo.getOnGuardOil()).add(null == rptCarWork.getOnGuardOil() ? BigDecimal.ZERO : rptCarWork.getOnGuardOil());
                    jxqWorkPlanDetailVo.setWorkOil(totalWorkOil);
                    jxqWorkPlanDetailVo.setOnGuardOil(totalOnGuardOil);
                    jxqWorkPlanDetailVo.setEmptyOil(totalOnGuardOil.subtract(totalWorkOil));

                    workTime = workTime.add(null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime());
                    onGuardWorkTime = onGuardWorkTime.add(null == rptCarWork.getOnGuardTime() ? BigDecimal.ZERO : rptCarWork.getOnGuardTime());

                    nowProgress = nowProgress.add(null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage());

                    onGuardMileage = onGuardMileage.add(null == rptCarWork.getOnGuardMileage() ? BigDecimal.ZERO : rptCarWork.getOnGuardMileage());
                }
                //每天每辆车有多少排班  （上午班 上午班）
                jxqWorkPlanDetailVo.setSetupTangCi(0);
//                jxqWorkPlanDetailVo.setWorkTangCi(0);
                Integer workTangCi = 0;
                Map<String, List<RptCarWork>> rptCarWorkByWorkDate = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                for (String workDate : rptCarWorkByWorkDate.keySet()) {
                    Integer allProgress = 0;

                    List<RptCarWork> rptCarWorkList2 = rptCarWorkByWorkDate.get(workDate);
                    if (null != rptCarWorkList2) {
                        Map<String, RptCarWork> rptCarWorkByShiftId = rptCarWorkList2.stream().collect(Collectors.groupingBy(RptCarWork::getShiftId, Collectors.collectingAndThen(toList(), values -> values.get(0))));
                        for (String shiftId : rptCarWorkByShiftId.keySet()) {
                            RptCarWork rptCarWork = rptCarWorkByShiftId.get(shiftId);
                            if (null != rptCarWork) {
                                BigDecimal singleProgress = (rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(rptCarWork.getAllProgress()).divide(new BigDecimal(rptCarWork.getTangCi()), BigDecimal.ROUND_HALF_UP);

                                BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);

                                int thisTangCi = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();
                                //作业趟次
                                workTangCi += thisTangCi;
                                //设定趟次
                                jxqWorkPlanDetailVo.setSetupTangCi(jxqWorkPlanDetailVo.getSetupTangCi() + rptCarWork.getTangCi());

                                allProgress = rptCarWork.getAllProgress();

                                planningMileage = planningMileage.add(new BigDecimal(allProgress));

                                if (thisTangCi < (null == rptCarWork.getTangCi() ? 0 : rptCarWork.getTangCi())) {
                                    jxqWorkPlanDetailVo.setStatus(1);
                                }
                            }
                        }
                    }
                }
                jxqWorkPlanDetailVo.setWorkTangCi(workTangCi);
            }

            jxqWorkPlanDetailVo.setPlanningMileage(planningMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqWorkPlanDetailVo.setWorkMileage(nowProgress.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqWorkPlanDetailVo.setOnGuardMileage(onGuardMileage.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqWorkPlanDetailVo.setEmptyMileage((onGuardMileage.subtract(nowProgress)).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));


            jxqWorkPlanDetailVo.setWorkTime(Tools.secondToTime(workTime.longValue()));
            jxqWorkPlanDetailVo.setOnGuardWorkTime(Tools.secondToTime(onGuardWorkTime.longValue()));
            jxqWorkPlanDetailVo.setEmptyWorkTime(Tools.secondToTime((onGuardWorkTime.subtract(workTime)).longValue()));

            jxqWorkPlanDetailVoList.add(jxqWorkPlanDetailVo);

        }

        ListSort(jxqWorkPlanDetailVoList);
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStatus())) {
            if (carWorkPlanCountTo.getStatus() == 3) {
                jxqWorkPlanDetailVoList = jxqWorkPlanDetailVoList.stream().filter(jxqWorkPlanDetailVo -> jxqWorkPlanDetailVo.getStatus() == 3).collect(toList());
                return jxqWorkPlanDetailVoList;
            } else if (carWorkPlanCountTo.getStatus() == 1) {
                jxqWorkPlanDetailVoList = jxqWorkPlanDetailVoList.stream().filter(jxqWorkPlanDetailVo -> jxqWorkPlanDetailVo.getStatus() == 1).collect(toList());
                return jxqWorkPlanDetailVoList;
            } else {
                return jxqWorkPlanDetailVoList;
            }
        }
        return jxqWorkPlanDetailVoList;
    }

    @Override
    public PageVo mechanicalWorkPlanDetailByCarVo(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<JxqWorkPlanDetailByCarVo> jxqWorkPlanDetailByCarVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        for (RptCarWork rptCarWork : rptCarWorkList) {
            JxqWorkPlanDetailByCarVo jxqWorkPlanDetailByCarVo = new JxqWorkPlanDetailByCarVo();
            jxqWorkPlanDetailByCarVo.setCarNumber(rptCarWork.getCarNum());
            jxqWorkPlanDetailByCarVo.setPlanName(rptCarWork.getPlanName());
            jxqWorkPlanDetailByCarVo.setWorkDate(rptCarWork.getWorkDate());
            jxqWorkPlanDetailByCarVo.setShift(rptCarWork.getShiftName());
            jxqWorkPlanDetailByCarVo.setWorkPlanStartTime(rptCarWork.getPlanStartTime());
            jxqWorkPlanDetailByCarVo.setWorkPlanEndTime(rptCarWork.getPlanEndTime());
            jxqWorkPlanDetailByCarVo.setStartWorkTime(rptCarWork.getWorkStartTime());
            jxqWorkPlanDetailByCarVo.setEndWorkTime(rptCarWork.getWorkEndTime());
            jxqWorkPlanDetailByCarVo.setWorkTime(Tools.secondToTime((null == rptCarWork.getWorkTime() ? 0 : rptCarWork.getWorkTime()).longValue()));
            jxqWorkPlanDetailByCarVo.setCompletePercent(new BigDecimal(rptCarWork.getCompletePercent()));
            jxqWorkPlanDetailByCarVo.setWorkMileage(rptCarWork.getWorkMileage().divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqWorkPlanDetailByCarVo.setPlanningMileage(new BigDecimal(rptCarWork.getAllProgress()).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
            jxqWorkPlanDetailByCarVoList.add(jxqWorkPlanDetailByCarVo);
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), jxqWorkPlanDetailByCarVoList);
    }

    private static void ListSortCount(List<JxqsWorkPlanCountVo> orderByJxqsWorkPlanCountVo) {
        //用Collections这个工具类传list进来排序
        Collections.sort(orderByJxqsWorkPlanCountVo, new Comparator<JxqsWorkPlanCountVo>() {
            @Override
            public int compare(JxqsWorkPlanCountVo o1, JxqsWorkPlanCountVo o2) {

                BigDecimal dt1 = null;
                BigDecimal dt2 = null;
                try {
                    dt1 = o1.getWorkMileage();
                    dt2 = o2.getWorkMileage();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return dt2.compareTo(dt1);
            }
        });
    }

    /**
     * 根据时间排序（其他排序如根据id排序也类似）
     *
     * @param orderByJxqWorkPlanDetailVo
     */
    private static void ListSort(List<JxqWorkPlanDetailVo> orderByJxqWorkPlanDetailVo) {
        //用Collections这个工具类传list进来排序
        Collections.sort(orderByJxqWorkPlanDetailVo, new Comparator<JxqWorkPlanDetailVo>() {
            @Override
            public int compare(JxqWorkPlanDetailVo o1, JxqWorkPlanDetailVo o2) {

                BigDecimal dt1 = null;
                BigDecimal dt2 = null;
                try {
                    dt1 = o1.getWorkMileage();
                    dt2 = o2.getWorkMileage();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return dt2.compareTo(dt1);
            }
        });
    }

    @Override
    public void insertGridSnapshot(CarWorkPlanCountTo carWorkPlanCountTo) throws ParseException {

        Calendar dayc1 = new GregorianCalendar();
        Calendar dayc2 = new GregorianCalendar();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date daystart = df.parse(carWorkPlanCountTo.getStartDate()); //按照yyyy-MM-dd格式转换为日期
        Date dayend = df.parse(carWorkPlanCountTo.getEndDate());
        dayc1.setTime(daystart); //设置calendar的日期
        dayc2.setTime(dayend);


        QueryWrapper<GridSnapshot> gridSnapshotQueryWrapper = new QueryWrapper<>();
        gridSnapshotQueryWrapper.lambda().eq(GridSnapshot::getDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        List<GridSnapshot> gridSnapshotList = gridSnapshotMapper.selectList(gridSnapshotQueryWrapper);
        for (; dayc1.compareTo(dayc2) <= 0; ) {
            Date time = dayc1.getTime();
            System.out.println(time);
            String format = df.format(time);
            System.out.println(format);
//            String date = dayc1.get(Calendar.YEAR) + "-" + +dayc1.get(Calendar.MONTH) + "-" + dayc1.get(Calendar.DATE);
//            System.out.println(date);
            for (GridSnapshot gridSnapshot : gridSnapshotList) {
                //dayc1在dayc2之前就循环
                System.out.println("网格复刻开始: =======================");
                GridSnapshot gridSnapshot1 = new GridSnapshot();
                BeanUtils.copyProperties(gridSnapshot, gridSnapshot1);
                gridSnapshot1.setId(StringUtils.uuid());
                gridSnapshot1.setDate(format);
                gridSnapshot1.setCreateTime(new Date());
                gridSnapshot1.setUpdateTime(new Date());
                gridSnapshotMapper.insert(gridSnapshot1);
                System.out.println("网格复刻结束: =======================");
            }
            dayc1.add(Calendar.DAY_OF_YEAR, 1);  //加1天
        }

    }


    @Override
    public void updateCarTrackNowProgress(CarWorkPlanCountTo carWorkPlanCountTo) {
        QueryWrapper<CarTrack> carTrackQueryWrappe = new QueryWrapper<>();
        carTrackQueryWrappe.lambda().eq(CarTrack::getWorkDay, carWorkPlanCountTo.getDate());
//        carTrackQueryWrappe.lambda().eq(CarTrack::getCarId, "a26f4b3a37344015a1023c93cd2fa30b");
//        carTrackQueryWrappe.lambda().eq(CarTrack::getIsInGrid, 1);
        List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrappe);
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        for (CarTrack currentCarTrack : carTrackList) {

            if (null != currentCarTrack.getShiftId()) {
                QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
                carWorkQueryWrapper.eq("car_id", currentCarTrack.getCarId());
//                carWorkQueryWrapper.eq("grid_id", currentCarTrack.getGridId());
                carWorkQueryWrapper.eq("work_date", StringUtils.dateToStr(currentCarTrack.getTimestamp(), "yyyy-MM-dd"));
                carWorkQueryWrapper.eq("shift_id", currentCarTrack.getShiftId());
                carWorkQueryWrapper.eq("work_type", 2);
                List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
                BigDecimal nowProgress = BigDecimal.ZERO;
                BigDecimal mileage = BigDecimal.ZERO;
                BigDecimal timeConsuming = BigDecimal.ZERO;
                BigDecimal oilConsumption = BigDecimal.ZERO;
                for (RptCarWork rptCarWork : rptCarWorkList) {
                    // in grid
                    boolean b = DistanceUtils.insideGrid(Double.parseDouble(currentCarTrack.getLng()), Double.parseDouble(currentCarTrack.getLat()), gridMap.get(rptCarWork.getGridId()));
                    if (b) {
                        //在时间内
                        if (rptCarWork.getPlanStartTime().compareTo(currentCarTrack.getTimestamp()) <= 0 && rptCarWork.getPlanEndTime().compareTo(currentCarTrack.getTimestamp()) >= 0) {
                            //开启喷头总进度   1  //喷头开启 暂时去掉   && currentCarTrack.getStatus() == 1
                            if (null != currentCarTrack.getStatus() && currentCarTrack.getStatus() == 1) {
                                nowProgress = rptCarWork.getWorkMileage().add(new BigDecimal(currentCarTrack.getDistanceBetween()));
                            }
                            if (currentCarTrack.getTimestamp().compareTo(rptCarWork.getPlanEndTime()) <= 0
                                    && currentCarTrack.getTimestamp().compareTo(rptCarWork.getPlanStartTime()) >= 0) {
                                rptCarWork.setWorkMileage(nowProgress);
                            }
//                            作业耗时
//                            timeConsuming = (null == rptCarWork.getTimeConsuming() ? BigDecimal.ZERO : rptCarWork.getTimeConsuming()).add(new BigDecimal((currentCarTrack.getTimestamp().getTime() - kfkHandleParam.getPreCarTrack().getTimestamp().getTime()) / 1000));
//                            rptCarWork.setTimeConsuming(timeConsuming);
//                            //作业油耗
//                            BigDecimal oilWear = MathUtils.getBigDecimal(kfkHandleParam.getPreCarTrack().getOilMass()).subtract(MathUtils.getBigDecimal(currentCarTrack.getOilMass())).add(new BigDecimal(currentCarTrack.getAddOilNum())).setScale(2, BigDecimal.ROUND_HALF_UP);
//                            oilConsumption = (null == rptCarWork.getOilConsumption() ? BigDecimal.ZERO : rptCarWork.getOilConsumption()).add(oilWear);
//                            rptCarWork.setOilConsumption(oilConsumption);
//
//                            mileage = (null == rptCarWork.getMileage() ? BigDecimal.ZERO : rptCarWork.getMileage()).add(new BigDecimal(currentCarTrack.getDistanceBetween()));
//                            rptCarWork.setMileage(mileage);

                            if (null == rptCarWork.getWorkStartTime() || "".equals(rptCarWork.getWorkStartTime())) {
                                rptCarWork.setWorkStartTime(currentCarTrack.getTimestamp());
                            }
                            rptCarWork.setWorkEndTime(currentCarTrack.getTimestamp());

                            rptCarWorkMapper.updateById(rptCarWork);
                        }
                    }
                }
            }
        }

    }


    @Override
    public List<LjqyWorkPlanCountVo> trashPickupWorkPlanCount(CarWorkPlanCountTo carWorkPlanCountTo) {
        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        if (null != carWorkPlanCountTo.getPlanIds() && !carWorkPlanCountTo.getPlanIds().isEmpty()) {
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanIds());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarNum, carWorkPlanCountTo.getCarNumber());
        }

        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        Map<String, List<RptCarWork>> rptCarWorkGroupByPlanId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Jobset jobset = jobsetMapper.selectById(4);

        List<LjqyWorkPlanCountVo> ljqyWorkPlanCountVoList = new ArrayList<>();
        for (String planId : rptCarWorkGroupByPlanId.keySet()) {

            List<RptCarWork> rptCarWorkList1 = rptCarWorkGroupByPlanId.get(planId);

            LjqyWorkPlanCountVo ljqyWorkPlanCountVo = new LjqyWorkPlanCountVo();
            Integer shouldPickupPoint = 0;
            Integer realityPickupPoint = 0;
            Set<String> carNumbers = new HashSet<>();
            if (null != rptCarWorkList1) {
                Map<String, List<RptCarWork>> rptCarWorkGroupByWorkDateMap = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
                ljqyWorkPlanCountVo.setPlanDays(rptCarWorkGroupByWorkDateMap.keySet().size());

                Integer completeDays = 0;
                for (String workDate : rptCarWorkGroupByWorkDateMap.keySet()) {
                    List<RptCarWork> rptCarWorkList2 = rptCarWorkGroupByWorkDateMap.get(workDate);
                    for (RptCarWork rptCarWork : rptCarWorkList2) {
                        if (rptCarWork.getCompletePercent() >= jobset.getPointOkThreshold()) {
                            completeDays++;
                        }
                    }

                }
                ljqyWorkPlanCountVo.setCompleteDays(completeDays);
                ljqyWorkPlanCountVo.setUnCompleteDays(ljqyWorkPlanCountVo.getPlanDays() - completeDays);

                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    carNumbers.add(rptCarWork.getCarNum());
                    ljqyWorkPlanCountVo.setCarId(rptCarWork.getCarId());
                    ljqyWorkPlanCountVo.setGridId(rptCarWork.getGridId());
                    ljqyWorkPlanCountVo.setGridName(rptCarWork.getGridName());
                    ljqyWorkPlanCountVo.setCarNumber(rptCarWork.getCarNum());
                    ljqyWorkPlanCountVo.setDeptName(null == sysDepartMap.get(rptCarWork.getDepartmentId()) ? "" : sysDepartMap.get(rptCarWork.getDepartmentId()).getDepartName());
                    ljqyWorkPlanCountVo.setPlanId(rptCarWork.getPlanId());
                    ljqyWorkPlanCountVo.setPlanName(rptCarWork.getPlanName());
                    ljqyWorkPlanCountVo.setCarType(rptCarWork.getCarType());
                    ljqyWorkPlanCountVo.setShouldPickupPoint(shouldPickupPoint += rptCarWork.getAllProgress());
                    ljqyWorkPlanCountVo.setRealityPickupPoint(realityPickupPoint += rptCarWork.getPickupPoint());
                    ljqyWorkPlanCountVo.setWorkMileage((null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                    ljqyWorkPlanCountVo.setTimeConsuming(Tools.secondToTime((null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime()).longValue()));
                    ljqyWorkPlanCountVo.setOilConsumption(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                    ljqyWorkPlanCountVo.setWorkCycle(null == rptCarWork.getWorkCycle() ? 1 : rptCarWork.getWorkCycle());
                    ljqyWorkPlanCountVo.setViolationsNumber(null == carViolationMap.get(rptCarWork.getCarId()) ? 0 : carViolationMap.get(rptCarWork.getCarId()).size());
                    ljqyWorkPlanCountVo.setAlarmNumber(null == alarmCarMap.get(rptCarWork.getCarId()) ? 0 : alarmCarMap.get(rptCarWork.getCarId()).size());
                }
                ljqyWorkPlanCountVo.setShiftCarNumbers(carNumbers);
            }
            ljqyWorkPlanCountVoList.add(ljqyWorkPlanCountVo);
        }
        return ljqyWorkPlanCountVoList;
    }


    @Override
    public List<LjqyWorkPlanDetailVo> trashPickupWorkPlanDetail(CarWorkPlanCountTo carWorkPlanCountTo) throws ParseException {
        long carSTime1 = System.currentTimeMillis();
        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        QueryWrapper<StateOilDevice> stateOilDeviceQueryWrapper = new QueryWrapper<>();
        stateOilDeviceQueryWrapper.lambda().between(StateOilDevice::getDeviceDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<StateOilDevice> stateOilDeviceList = stateOilDeviceMapper.selectList(stateOilDeviceQueryWrapper);
        Map<String, List<StateOilDevice>> stateOilDeviceMap = stateOilDeviceList.stream().collect(Collectors.groupingBy(StateOilDevice::getCarId));

        long carSTime2 = System.currentTimeMillis();
        log.info("=====carLocationHandle1=执行时间(毫秒)：{}", carSTime2 - carSTime1);

//        QueryWrapper<RptCarWorkFacility> rptCarWorkFacilityQueryWrapper = new QueryWrapper<>();
//        rptCarWorkFacilityQueryWrapper.lambda().isNotNull(RptCarWorkFacility::getCleanType);
//        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(rptCarWorkFacilityQueryWrapper);
//        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));

//        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
//        carTrackQueryWrapper.lambda().between(CarTrack::getTimestamp, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
//        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 3);
//        carTrackQueryWrapper.lambda().eq(CarTrack::getCleanType, 0);
//        carTrackQueryWrapper.lambda().groupBy(CarTrack::getFacilityId);
//        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
//        List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrapper);
//        Map<String, List<CarTrack>> carTrackStreamList = carTrackList.stream().collect(Collectors.groupingBy(CarTrack::getCarId));

        long carSTime3 = System.currentTimeMillis();
        log.info("=====carLocationHandle2=执行时间(毫秒)：{}", carSTime3 - carSTime2);

        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        long carSTime4 = System.currentTimeMillis();
        log.info("=====carLocationHandle3=执行时间(毫秒)：{}", carSTime4 - carSTime3);

        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        alarmCarList.forEach(alarmCar -> {
            alarmCar.setDate(StringUtils.dateToStr(alarmCar.getAlarmTime(), "yyyy-MM-dd"));
        });
        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().filter(alarmCar -> null != alarmCar.getDate()).collect(Collectors.groupingBy(AlarmCar::getDate));

        long carSTime5 = System.currentTimeMillis();
        log.info("=====carLocationHandle4=执行时间(毫秒)：{}", carSTime5 - carSTime4);

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        carViolationList.forEach(carViolation -> {
            carViolation.setDate(StringUtils.dateToStr(carViolation.getTime(), "yyyy-MM-dd"));
        });
        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().filter(carViolation -> null != carViolation.getDate()).collect(Collectors.groupingBy(CarViolation::getDate));

        long carSTime6 = System.currentTimeMillis();
        log.info("=====carLocationHandle5=执行时间(毫秒)：{}", carSTime6 - carSTime5);

        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        Jobset jobset = jobsetMapper.selectById(4);

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        if (null != carWorkPlanCountTo.getWorkDates() && !carWorkPlanCountTo.getWorkDates().isEmpty()) {
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getWorkDate, carWorkPlanCountTo.getWorkDates());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getCarNum, carWorkPlanCountTo.getCarNumber());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkGroupByPlanId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));
        long carSTime7 = System.currentTimeMillis();
        log.info("=====carLocationHandle6=执行时间(毫秒)：{}", carSTime7 - carSTime6);

        List<LjqyWorkPlanDetailVo> ljqyWorkPlanDetailVoList = new ArrayList<>();

        for (String carId : rptCarWorkGroupByPlanId.keySet()) {

            List<RptCarWork> rptCarWorkList1 = rptCarWorkGroupByPlanId.get(carId);

//            Set<String> shouldPickupPointIds = new HashSet<>();
//            Set<String> realityPickupPointIds = new HashSet<>();
//
//            Set<String> shouldPickupPointNames = new HashSet<>();
//            Set<String> realityPickupPointNames = new HashSet<>();
//            Set<String> unmadePickupPointNames = new HashSet<>();
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {

                    LjqyWorkPlanDetailVo ljqyWorkPlanDetailVo = new LjqyWorkPlanDetailVo();

                    ljqyWorkPlanDetailVo.setShouldPickupPoint(rptCarWork.getAllProgress());
                    ljqyWorkPlanDetailVo.setRealityPickupPoint(rptCarWork.getPickupPoint());
                    ljqyWorkPlanDetailVo.setUnmadePickupPoint(rptCarWork.getAllProgress() - rptCarWork.getPickupPoint());

//                    if (null != rptCarWork.getSjdIds()) {
//                        String[] split = rptCarWork.getSjdIds().split(",");
//                        for (String id : split) {
//                            TlFacilities tlFacilities = tlFacilitiesMap.get(id);
//                            if (null != tlFacilities) {
//                                //应清
//                                shouldPickupPointIds.add(tlFacilities.getId());
//                                shouldPickupPointNames.add(tlFacilities.getFaclname());
//                            }
//                        }
//                    }
//
//                    ljqyWorkPlanDetailVo.setShouldPickupPointNames(shouldPickupPointNames);
//                    if (null != carTrackStreamList.get(rptCarWork.getCarId())) {
//                        for (CarTrack carTrack : carTrackStreamList.get(rptCarWork.getCarId())) {
//                            String facilityIds = carTrack.getFacilityId();
//                            if (null != facilityIds) {
//                                for (String faclId : facilityIds.split(",")) {
//                                    TlFacilities tlFacilities = tlFacilitiesMap.get(faclId);
//                                    if (null != tlFacilities) {
//                                        realityPickupPointIds.add(tlFacilities.getId());
//                                        realityPickupPointNames.add(tlFacilities.getFaclname());
//                                    }
//                                }
//                            }
//                        }
//                    }
//
//                    ljqyWorkPlanDetailVo.setRealityPickupPointNames(realityPickupPointNames);
//
//                    List<String> reduce = shouldPickupPointIds.stream().filter(item -> !realityPickupPointIds.contains(item)).collect(toList());
//                    for (String id : reduce) {
//                        TlFacilities tlFacilities = tlFacilitiesMap.get(id);
//                        if (null != tlFacilities) {
//                            unmadePickupPointNames.add(tlFacilities.getFaclname());
//                        }
//                    }
//                    ljqyWorkPlanDetailVo.setUnmadePickupPointNames(unmadePickupPointNames);

                    ljqyWorkPlanDetailVo.setCarId(rptCarWork.getCarId());
                    ljqyWorkPlanDetailVo.setCarNumber(rptCarWork.getCarNum());
                    ljqyWorkPlanDetailVo.setGridId(rptCarWork.getGridId());
                    ljqyWorkPlanDetailVo.setGridName(rptCarWork.getGridName());
                    ljqyWorkPlanDetailVo.setCarType(rptCarWork.getCarType());
                    ljqyWorkPlanDetailVo.setDeptName(null == sysDepartMap.get(rptCarWork.getDepartmentId()) ? "" : sysDepartMap.get(rptCarWork.getDepartmentId()).getDepartName());
                    ljqyWorkPlanDetailVo.setDate(rptCarWork.getWorkDate());
                    ljqyWorkPlanDetailVo.setPlanId(rptCarWork.getPlanId());
                    ljqyWorkPlanDetailVo.setPlanName(rptCarWork.getPlanName());
                    ljqyWorkPlanDetailVo.setWorkCycle(rptCarWork.getWorkCycle());

                    if (null != ljqyWorkPlanDetailVo.getShouldPickupPoint() && ljqyWorkPlanDetailVo.getShouldPickupPoint() > 0) {
                        BigDecimal completePercent = (new BigDecimal(null == ljqyWorkPlanDetailVo.getRealityPickupPoint() ? 0 : ljqyWorkPlanDetailVo.getRealityPickupPoint()).divide(new BigDecimal(ljqyWorkPlanDetailVo.getShouldPickupPoint()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_UP);
                        if (completePercent.compareTo(new BigDecimal(jobset.getPointOkThreshold())) >= 0) {
                            ljqyWorkPlanDetailVo.setStatus(3);
                        } else {
                            ljqyWorkPlanDetailVo.setStatus(1);
                        }
                        ljqyWorkPlanDetailVo.setClearanceCompletionRate(completePercent);
                    }


                    ljqyWorkPlanDetailVo.setExpectTime(null == rptCarWork.getExpectWorkTime() ? null : rptCarWork.getExpectWorkTime());

                    List<AlarmCar> alarmCarList1 = alarmCarMap.get(rptCarWork.getWorkDate());
                    if (null != alarmCarList1) {
                        Map<String, List<AlarmCar>> alarmCarListMap = alarmCarList1.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));
                        List<AlarmCar> alarmCarList2 = alarmCarListMap.get(carId);
                        if (null != alarmCarList2) {
                            ljqyWorkPlanDetailVo.setAlarmNumber(alarmCarList2.size());
                        } else {
                            ljqyWorkPlanDetailVo.setAlarmNumber(0);
                        }
                    }


                    List<CarViolation> carViolationList1 = carViolationMap.get(rptCarWork.getWorkDate());
                    if (null != carViolationList1) {
                        Map<String, List<CarViolation>> carViolationListMap = carViolationList1.stream().collect(Collectors.groupingBy(CarViolation::getCarId));
                        List<CarViolation> carViolationList2 = carViolationListMap.get(carId);
                        if (null != carViolationList2) {
                            ljqyWorkPlanDetailVo.setViolationsNumber(carViolationList2.size());
                        } else {
                            ljqyWorkPlanDetailVo.setViolationsNumber(0);
                        }
                    }


                    ljqyWorkPlanDetailVo.setExpectOil(null == rptCarWork.getExpectOilConsumption() ? null : rptCarWork.getExpectOilConsumption());
                    ljqyWorkPlanDetailVo.setPlanMileage(null == gridMap.get(rptCarWork.getGridId()) ? 0d : (null == gridMap.get(rptCarWork.getGridId()).getKm() ? 0 : gridMap.get(rptCarWork.getGridId()).getKm()));
                    ljqyWorkPlanDetailVo.setShiftName(rptCarWork.getShiftName());
                    ljqyWorkPlanDetailVo.setShiftStartDate(rptCarWork.getPlanStartTime());
                    ljqyWorkPlanDetailVo.setShiftEndDate(rptCarWork.getPlanEndTime());

//
                    List<StateOilDevice> stateOilDeviceList1 = stateOilDeviceMap.get(rptCarWork.getCarId());
                    if (null != stateOilDeviceList1) {
                        Map<Date, List<StateOilDevice>> stateOilDeviceByDateMap = stateOilDeviceList1.stream().collect(Collectors.groupingBy(StateOilDevice::getDeviceDate));

                        List<StateOilDevice> stateOilDeviceList2 = stateOilDeviceByDateMap.get(DateUtil.parse(rptCarWork.getWorkDate(), "yyyy-MM-dd"));
                        if (null != stateOilDeviceList2) {
                            for (StateOilDevice stateOilDevice : stateOilDeviceList2) {

                                ljqyWorkPlanDetailVo.setTravelMileage((null == stateOilDevice.getClockMileage() ? BigDecimal.ZERO : stateOilDevice.getClockMileage()));

                                ljqyWorkPlanDetailVo.setTravelOil((null == stateOilDevice.getUseOilNum() ? BigDecimal.ZERO : stateOilDevice.getUseOilNum()));

                                BigDecimal travelTime = (null == stateOilDevice.getMoveSecondSum() ? BigDecimal.ZERO : stateOilDevice.getMoveSecondSum());

                                ljqyWorkPlanDetailVo.setTravelTimeStr(Tools.secondToTime((travelTime.divide(new BigDecimal(1000))).longValue()));

                                ljqyWorkPlanDetailVo.setWorkStartTime(stateOilDevice.getStartDate());
                                ljqyWorkPlanDetailVo.setWorkEndTime(stateOilDevice.getEndDate());
                            }
                        }
                    }

                    ljqyWorkPlanDetailVoList.add(ljqyWorkPlanDetailVo);

                }
            }

        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStatus())) {
            if (carWorkPlanCountTo.getStatus() == 3) {
                ljqyWorkPlanDetailVoList = ljqyWorkPlanDetailVoList.stream().filter(ljqyWorkPlanDetailVo -> ljqyWorkPlanDetailVo.getStatus() == 3).collect(toList());
                return ljqyWorkPlanDetailVoList;
            } else if (carWorkPlanCountTo.getStatus() == 1) {
                ljqyWorkPlanDetailVoList = ljqyWorkPlanDetailVoList.stream().filter(ljqyWorkPlanDetailVo -> ljqyWorkPlanDetailVo.getStatus() == 1).collect(toList());
                return ljqyWorkPlanDetailVoList;
            } else {
                return ljqyWorkPlanDetailVoList;
            }
        } else {
            return ljqyWorkPlanDetailVoList;
        }

    }

    @Override
    public PageVo trashPickupWorkPlanDetailByCar(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<LjqyWorkPlanDetailByCarVo> ljqyWorkPlanDetailByCarVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        for (RptCarWork rptCarWork : rptCarWorkList) {
            LjqyWorkPlanDetailByCarVo ljqyWorkPlanDetailByCarVo = new LjqyWorkPlanDetailByCarVo();
            ljqyWorkPlanDetailByCarVo.setCarNumber(rptCarWork.getCarNum());
            ljqyWorkPlanDetailByCarVo.setPlanName(rptCarWork.getPlanName());
            ljqyWorkPlanDetailByCarVo.setShiftName(rptCarWork.getShiftName());
            ljqyWorkPlanDetailByCarVo.setPlanStartTime(rptCarWork.getPlanStartTime());
            ljqyWorkPlanDetailByCarVo.setPlanEndTime(rptCarWork.getPlanEndTime());
            ljqyWorkPlanDetailByCarVo.setWorkStartTime(rptCarWork.getWorkStartTime());
            ljqyWorkPlanDetailByCarVo.setWorkEndTime(rptCarWork.getWorkEndTime());
            ljqyWorkPlanDetailByCarVo.setWorkTime(Tools.secondToTime((null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime()).longValue()));
            ljqyWorkPlanDetailByCarVo.setClearanceCompletionRate(new BigDecimal(rptCarWork.getCompletePercent()));
            ljqyWorkPlanDetailByCarVo.setDate(rptCarWork.getWorkDate());
            ljqyWorkPlanDetailByCarVoList.add(ljqyWorkPlanDetailByCarVo);
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljqyWorkPlanDetailByCarVoList);


    }


    @Override
    @DataScope(deptAlias = "department_id", userAlias = "")
    public PageVo garbageTransitWorkPlanCount(BaseEntity entity, CarWorkPlanCountTo carWorkPlanCountTo) {

        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId));

        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(new QueryWrapper<>());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter2 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 2).collect(toList());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter3 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 3).collect(toList());
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap2 = rptCarWorkFacilityFilter2.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap3 = rptCarWorkFacilityFilter3.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));


        QueryWrapper<RptCarWorkDetail> rptCarWorkDetailQueryWrapper = new QueryWrapper<>();
        rptCarWorkDetailQueryWrapper.lambda().between(RptCarWorkDetail::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<RptCarWorkDetail> rptCarWorkDetailList = rptCarWorkDetailMapper.selectList(rptCarWorkDetailQueryWrapper);
        Map<String, List<RptCarWorkDetail>> rptCarWorkDetailByFacility = rptCarWorkDetailList.stream().collect(Collectors.groupingBy(RptCarWorkDetail::getRptCarWorkFacilityId));


        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 4);
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarNum, carWorkPlanCountTo.getCarNumber());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            rptCarWorkQueryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        Jobset jobset = jobsetMapper.selectById(5);

        List<LjzyWorkPlanCountVo> ljzyWorkPlanCountVoList = new ArrayList<>();
        Map<String, List<RptCarWork>> rptCarWorkGroupByPlanId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));

        for (String planId : rptCarWorkGroupByPlanId.keySet()) {
            List<RptCarWork> rptCarWorkList1 = rptCarWorkGroupByPlanId.get(planId);
            LjzyWorkPlanCountVo ljzyWorkPlanCountVo = new LjzyWorkPlanCountVo();

            Integer completeDays = 0;
            Map<String, List<RptCarWork>> rptCarWorkGroupByWorkDate1 = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getWorkDate));
            ljzyWorkPlanCountVo.setPlanDays(rptCarWorkGroupByWorkDate1.keySet().size());
            Integer number = 0;
            for (String workDate : rptCarWorkGroupByWorkDate1.keySet()) {
                Boolean isFinish = true;
                List<RptCarWork> rptCarWorkList3 = rptCarWorkGroupByWorkDate1.get(workDate);
                if (null != rptCarWorkList3) {
                    for (RptCarWork rptCarWork : rptCarWorkList3) {
                        List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityMap2.get(rptCarWork.getId());
                        if (null != rptCarWorkFacilityList1) {
                            for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                                List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByFacility.get(rptCarWorkFacility.getRptCarWorkId());
                                if (null != rptCarWorkDetailList1) {
                                    number = rptCarWorkDetailList1.size();
                                }
                            }
                        }
                        List<RptCarWorkFacility> rptCarWorkFacilityList2 = rptCarWorkFacilityMap3.get(rptCarWork.getId());
                        if (null != rptCarWorkFacilityList2) {
                            for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList2) {
                                List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByFacility.get(rptCarWorkFacility.getRptCarWorkId());
                                if (null != rptCarWorkDetailList1) {
                                    number = rptCarWorkDetailList1.size();
                                }
                            }
                        }
                    }
                    Map<String, List<RptCarWork>> rptCarWorkByShiftId = rptCarWorkList3.stream().collect(Collectors.groupingBy(RptCarWork::getShiftId));
                    for (String shiftId : rptCarWorkByShiftId.keySet()) {
                        BigDecimal expectMileage = BigDecimal.ZERO;
                        BigDecimal mileage = BigDecimal.ZERO;
                        List<RptCarWork> rptCarWorkList4 = rptCarWorkByShiftId.get(shiftId);
                        if (null != rptCarWorkList4) {
                            Map<String, List<RptCarWork>> rptCarWorkByCarId = rptCarWorkList4.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));
                            for (String carId : rptCarWorkByCarId.keySet()) {
                                List<RptCarWork> rptCarWorkList2 = rptCarWorkByCarId.get(carId);
                                for (RptCarWork rptCarWork : rptCarWorkList2) {
                                    expectMileage = new BigDecimal(jobset.getPointOkThreshold()).multiply((null == rptCarWork.getExpectMileage() ? BigDecimal.ZERO : rptCarWork.getExpectMileage()).multiply(new BigDecimal(1000))).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);
                                    mileage = null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage();
                                }
                            }

                            if (mileage.compareTo(expectMileage) < 0 && number <= 0) {
                                isFinish = false;
                            }
                        }
                    }
                    if (isFinish) {
                        completeDays++;
                    }
                }
            }
            ljzyWorkPlanCountVo.setCompleteDays(completeDays);
            ljzyWorkPlanCountVo.setUnCompleteDays(ljzyWorkPlanCountVo.getPlanDays() - completeDays);

            for (RptCarWork rptCarWork : rptCarWorkList1) {
                List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityMap2.get(rptCarWork.getId());
                if (null != rptCarWorkFacilityList1) {
                    for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                        List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByFacility.get(rptCarWorkFacility.getId());
                        if (null != rptCarWorkDetailList1) {
                            ljzyWorkPlanCountVo.setCollectStandNumber(rptCarWorkDetailList1.size());
                        }
                    }
                }
                List<RptCarWorkFacility> rptCarWorkFacilityList2 = rptCarWorkFacilityMap3.get(rptCarWork.getId());
                if (null != rptCarWorkFacilityList2) {
                    for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList2) {
                        List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByFacility.get(rptCarWorkFacility.getId());
                        if (null != rptCarWorkDetailList1) {
                            ljzyWorkPlanCountVo.setProcessStandNumber(rptCarWorkDetailList1.size());
                        }
                    }
                }
                ljzyWorkPlanCountVo.setPlanId(rptCarWork.getPlanId());
                ljzyWorkPlanCountVo.setCarId(rptCarWork.getCarId());
                ljzyWorkPlanCountVo.setPlanName(rptCarWork.getPlanName());
                ljzyWorkPlanCountVo.setCarNumber(rptCarWork.getCarNum());
                ljzyWorkPlanCountVo.setCarType(rptCarWork.getCarType());
                ljzyWorkPlanCountVo.setDeptName(null == sysDepartMap.get(rptCarWork.getDepartmentId()) ? "" : sysDepartMap.get(rptCarWork.getDepartmentId()).getDepartName());
                ljzyWorkPlanCountVo.setAlarmNumber(null == alarmCarMap.get(rptCarWork.getCarId()) ? 0 : alarmCarMap.get(rptCarWork.getCarId()).size());
                ljzyWorkPlanCountVo.setViolationsNumber(null == carViolationMap.get(rptCarWork.getCarId()) ? 0 : carViolationMap.get(rptCarWork.getCarId()).size());
                ljzyWorkPlanCountVo.setMileage(null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage());
                ljzyWorkPlanCountVo.setExpectMileage(null == rptCarWork.getExpectMileage() ? BigDecimal.ZERO : rptCarWork.getExpectMileage());
                ljzyWorkPlanCountVo.setOilConsumption(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                ljzyWorkPlanCountVo.setTimeConsuming(Tools.secondToTime((null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime()).longValue()));
            }
            ljzyWorkPlanCountVoList.add(ljzyWorkPlanCountVo);
        }

        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanCountVoList);
    }

    @Override
    public PageVo garbageTransitWorkPlanDetail(CarWorkPlanCountTo carWorkPlanCountTo) {

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        Map<String, List<AlarmCar>> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId));

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        Map<String, List<CarViolation>> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId));

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 4);
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarType())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarType, carWorkPlanCountTo.getCarType());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarNumber())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarNum, carWorkPlanCountTo.getCarNumber());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getDepartmentId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getDepartmentId, carWorkPlanCountTo.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getGridName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getGridName, carWorkPlanCountTo.getGridName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getPlanName())) {
            rptCarWorkQueryWrapper.lambda().like(RptCarWork::getPlanName, carWorkPlanCountTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        List<LjzyWorkPlanDetailVo> ljzyWorkPlanDetailVoList = new ArrayList<>();

        Map<String, List<RptCarWork>> rptCarWorkGroupPlanId = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getPlanId));
        for (String planId : rptCarWorkGroupPlanId.keySet()) {

            List<RptCarWork> rptCarWorkList1 = rptCarWorkGroupPlanId.get(planId);

            Map<String, List<RptCarWork>> rptCarWorkGroupByShiftId = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getShiftId));

            for (String shiftId : rptCarWorkGroupByShiftId.keySet()) {

                List<RptCarWork> rptCarWorkList2 = rptCarWorkGroupByShiftId.get(shiftId);
                LjzyWorkPlanDetailVo ljzyWorkPlanDetailVo = new LjzyWorkPlanDetailVo();

                for (RptCarWork rptCarWork : rptCarWorkList2) {
                    ljzyWorkPlanDetailVo.setCarId(rptCarWork.getCarId());
                    ljzyWorkPlanDetailVo.setPlanId(rptCarWork.getPlanId());
                    ljzyWorkPlanDetailVo.setCarNumber(rptCarWork.getCarNum());
                    ljzyWorkPlanDetailVo.setCarType(rptCarWork.getCarType());
                    ljzyWorkPlanDetailVo.setDate(rptCarWork.getWorkDate());
                    ljzyWorkPlanDetailVo.setDeptName(null == sysDepartMap.get(rptCarWork.getDepartmentId()) ? "" : sysDepartMap.get(rptCarWork.getDepartmentId()).getDepartName());
                    ljzyWorkPlanDetailVo.setDate(rptCarWork.getWorkDate());
                    ljzyWorkPlanDetailVo.setPlanName(rptCarWork.getPlanName());
                    ljzyWorkPlanDetailVo.setWorkCycle(rptCarWork.getWorkCycle());
                    ljzyWorkPlanDetailVo.setStatus(rptCarWork.getStatus());
                    ljzyWorkPlanDetailVo.setClearanceCompletionRate(rptCarWork.getCompletePercent());
                    ljzyWorkPlanDetailVo.setShouldPickupPoint(rptCarWork.getAllProgress());
                    ljzyWorkPlanDetailVo.setRealityPickupPoint(rptCarWork.getPickupPoint());
                    ljzyWorkPlanDetailVo.setWorkStartTime(rptCarWork.getWorkStartTime());
                    ljzyWorkPlanDetailVo.setWorkEndTime(rptCarWork.getWorkEndTime());
                    ljzyWorkPlanDetailVo.setAlarmNumber(null == alarmCarMap.get(rptCarWork.getCarId()) ? 0 : alarmCarMap.get(rptCarWork.getCarId()).size());
                    ljzyWorkPlanDetailVo.setViolationsNumber(null == carViolationMap.get(rptCarWork.getCarId()) ? 0 : carViolationMap.get(rptCarWork.getCarId()).size());
                    ljzyWorkPlanDetailVo.setOilConsumption(null == rptCarWork.getWorkOil() ? BigDecimal.ZERO : rptCarWork.getWorkOil());
                    ljzyWorkPlanDetailVo.setExpectMileage(null == rptCarWork.getExpectMileage() ? BigDecimal.ZERO : rptCarWork.getExpectMileage());
                    ljzyWorkPlanDetailVo.setWorkMileage((null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                    ljzyWorkPlanDetailVo.setMileage((null == rptCarWork.getWorkMileage() ? BigDecimal.ZERO : rptCarWork.getWorkMileage()).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP));
                    ljzyWorkPlanDetailVo.setShiftName(rptCarWork.getShiftName());
                    ljzyWorkPlanDetailVo.setShiftStartDate(rptCarWork.getPlanStartTime());
                    ljzyWorkPlanDetailVo.setShiftEndDate(rptCarWork.getPlanEndTime());
                    ljzyWorkPlanDetailVo.setTimeConsuming(Tools.secondToTime((null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime()).longValue()));
                }
                ljzyWorkPlanDetailVoList.add(ljzyWorkPlanDetailVo);
            }
        }
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStatus())) {
            if (carWorkPlanCountTo.getStatus() == 3) {
                ljzyWorkPlanDetailVoList = ljzyWorkPlanDetailVoList.stream().filter(ljzyWorkPlanDetailVo -> ljzyWorkPlanDetailVo.getStatus() == 3).collect(toList());
                return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanDetailVoList);
            } else if (carWorkPlanCountTo.getStatus() == 1) {
                ljzyWorkPlanDetailVoList = ljzyWorkPlanDetailVoList.stream().filter(ljzyWorkPlanDetailVo -> ljzyWorkPlanDetailVo.getStatus() == 1).collect(toList());
                return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanDetailVoList);
            } else {
                return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanDetailVoList);
            }
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanDetailVoList);
    }

    @Override
    public PageVo garbageTransitWorkPlanByCollectStand(CarWorkPlanCountTo carWorkPlanCountTo) {

        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(new QueryWrapper<>());
        Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap = carPlanCollectStandList.stream().collect(Collectors.groupingBy(CarPlanCollectStand::getCarPlanId));

        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(new QueryWrapper<>());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter2 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 2).collect(toList());
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap2 = rptCarWorkFacilityFilter2.stream().collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));

        QueryWrapper<RptCarWorkDetail> rptCarWorkDetailQueryWrapper = new QueryWrapper<>();
        rptCarWorkDetailQueryWrapper.lambda().between(RptCarWorkDetail::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<RptCarWorkDetail> rptCarWorkDetailList = rptCarWorkDetailMapper.selectList(rptCarWorkDetailQueryWrapper);
        Map<String, List<RptCarWorkDetail>> rptCarWorkDetailByRptCarWorkId = rptCarWorkDetailList.stream().collect(Collectors.groupingBy(RptCarWorkDetail::getRptCarWorkId));

//        车牌号、排班计划名称、序号、日期、处理站（名称）、处理开始时间（定位进入处理站的时间）、处理结束时间
        List<CarPlanCollectStandVo> carPlanCollectStandVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        for (RptCarWork rptCarWork : rptCarWorkList) {
            CarPlanCollectStandVo carPlanCollectStandVo = new CarPlanCollectStandVo();
            carPlanCollectStandVo.setCarNumber(rptCarWork.getCarNum());
            carPlanCollectStandVo.setPlanName(rptCarWork.getPlanName());
            carPlanCollectStandVo.setWorkDate(rptCarWork.getWorkDate());

            List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByRptCarWorkId.get(rptCarWork.getId());
            if (null != rptCarWorkDetailList1) {
                carPlanCollectStandVo.setWorkStartTime(rptCarWorkDetailList1.get(0).getStartTime());
                carPlanCollectStandVo.setWorkEndTime(rptCarWorkDetailList1.get(0).getEndTime());
                Long time = (rptCarWorkDetailList1.get(0).getEndTime().getTime() - rptCarWorkDetailList1.get(0).getStartTime().getTime()) / 1000;
                carPlanCollectStandVo.setTimeConsumption(Tools.secondToTime(time));
            }

            List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityMap2.get(rptCarWork.getId());
            if (null != rptCarWorkFacilityList1) {
                carPlanCollectStandVo.setCarPlanCollectStandList(rptCarWorkFacilityList1);
            }
            carPlanCollectStandVoList.add(carPlanCollectStandVo);
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanCollectStandVoList);
    }

    @Override
    public PageVo garbageTransitWorkPlanByProcessStand(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(new QueryWrapper<>());
        Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap = carPlanProcessStandList.stream().collect(Collectors.groupingBy(CarPlanProcessStand::getCarPlanId));

        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(new QueryWrapper<>());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter3 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 3).collect(toList());
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap3 = rptCarWorkFacilityFilter3.stream().collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));

        QueryWrapper<RptCarWorkDetail> rptCarWorkDetailQueryWrapper = new QueryWrapper<>();
        rptCarWorkDetailQueryWrapper.lambda().between(RptCarWorkDetail::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<RptCarWorkDetail> rptCarWorkDetailList = rptCarWorkDetailMapper.selectList(rptCarWorkDetailQueryWrapper);
        Map<String, List<RptCarWorkDetail>> rptCarWorkDetailByRptCarWorkId = rptCarWorkDetailList.stream().collect(Collectors.groupingBy(RptCarWorkDetail::getRptCarWorkId));

        List<CarPlanCollectStandVo> carPlanCollectStandVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        for (RptCarWork rptCarWork : rptCarWorkList) {
            CarPlanCollectStandVo carPlanCollectStandVo = new CarPlanCollectStandVo();
            carPlanCollectStandVo.setCarNumber(rptCarWork.getCarNum());
            carPlanCollectStandVo.setPlanName(rptCarWork.getPlanName());
            carPlanCollectStandVo.setWorkDate(rptCarWork.getWorkDate());

            List<RptCarWorkDetail> rptCarWorkDetailList1 = rptCarWorkDetailByRptCarWorkId.get(rptCarWork.getId());
            if (null != rptCarWorkDetailList1) {
                carPlanCollectStandVo.setWorkStartTime(rptCarWorkDetailList1.get(0).getStartTime());
                carPlanCollectStandVo.setWorkEndTime(rptCarWorkDetailList1.get(0).getEndTime());
                Long time = (rptCarWorkDetailList1.get(0).getEndTime().getTime() - rptCarWorkDetailList1.get(0).getStartTime().getTime()) / 1000;
                carPlanCollectStandVo.setTimeConsumption(Tools.secondToTime(time));
            }

            List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityMap3.get(rptCarWork.getId());
            if (null != rptCarWorkFacilityList1) {
                carPlanCollectStandVo.setCarPlanCollectStandList(rptCarWorkFacilityList1);
            }
            carPlanCollectStandVoList.add(carPlanCollectStandVo);
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanCollectStandVoList);

    }

    @Override
    public PageVo garbageTransitWorkPlanDetailByCar(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<LjzyWorkPlanDetailByCarVo> ljzyWorkPlanDetailByCarVoList = new ArrayList<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getPlanId, carWorkPlanCountTo.getPlanId());
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        rptCarWorkQueryWrapper.lambda().orderByAsc(RptCarWork::getWorkDate);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        QueryWrapper<StateOilDevice> stateOilDeviceQueryWrapper = new QueryWrapper<>();
        stateOilDeviceQueryWrapper.lambda().between(StateOilDevice::getDeviceDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<StateOilDevice> stateOilDeviceList = stateOilDeviceMapper.selectList(stateOilDeviceQueryWrapper);
        Map<String, List<StateOilDevice>> stateOilDeviceByCarId = stateOilDeviceList.stream().collect(Collectors.groupingBy(StateOilDevice::getCarId));


        for (RptCarWork rptCarWork : rptCarWorkList) {
            LjzyWorkPlanDetailByCarVo ljzyWorkPlanDetailByCarVo = new LjzyWorkPlanDetailByCarVo();
            ljzyWorkPlanDetailByCarVo.setCarNumber(rptCarWork.getCarNum());
            ljzyWorkPlanDetailByCarVo.setPlanName(rptCarWork.getPlanName());
            ljzyWorkPlanDetailByCarVo.setShiftName(rptCarWork.getShiftName());
            ljzyWorkPlanDetailByCarVo.setPlanStartTime(rptCarWork.getPlanStartTime());
            ljzyWorkPlanDetailByCarVo.setPlanEndTime(rptCarWork.getPlanEndTime());
            ljzyWorkPlanDetailByCarVo.setWorkStartTime(rptCarWork.getWorkStartTime());
            ljzyWorkPlanDetailByCarVo.setWorkEndTime(rptCarWork.getWorkEndTime());
            ljzyWorkPlanDetailByCarVo.setStatus(rptCarWork.getStatus());
            ljzyWorkPlanDetailByCarVo.setWorkTime(Tools.secondToTime((null == rptCarWork.getWorkTime() ? BigDecimal.ZERO : rptCarWork.getWorkTime()).longValue()));
            ljzyWorkPlanDetailByCarVo.setClearanceCompletionRate(new BigDecimal(rptCarWork.getCompletePercent()));
            ljzyWorkPlanDetailByCarVo.setDate(rptCarWork.getWorkDate());

            List<StateOilDevice> stateOilDeviceList1 = stateOilDeviceByCarId.get(rptCarWork.getCarId());
            if (null != stateOilDeviceList1) {
                ljzyWorkPlanDetailByCarVo.setOutCarTime(stateOilDeviceList1.get(0).getStartDate());
                ljzyWorkPlanDetailByCarVo.setCloseCarTime(stateOilDeviceList1.get(0).getEndDate());
            }

            ljzyWorkPlanDetailByCarVoList.add(ljzyWorkPlanDetailByCarVo);
        }
        return Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), ljzyWorkPlanDetailByCarVoList);


    }


    @Override
    public List<TreeEntity> getCarShiftStatus(CarWorkPlanCountTo carWorkPlanCountTo) {
        Set<String> carIdList = new HashSet<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getStartDate()) && StringUtils.isNotEmpty(carWorkPlanCountTo.getEndDate())) {
            rptCarWorkQueryWrapper.lambda().le(RptCarWork::getPlanStartTime, carWorkPlanCountTo.getStartDate());
            rptCarWorkQueryWrapper.lambda().ge(RptCarWork::getPlanEndTime, carWorkPlanCountTo.getStartDate());
        }
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(rptCarWork -> {
            carIdList.add(rptCarWork.getCarId());
        });
        List<TreeEntity> trees = new ArrayList<>();
        if (!carIdList.isEmpty()) {
            QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
            tlCarQueryWrapper.lambda().in(TlCar::getId, carIdList);
            List<TlCar> tlCarList = tlCarMapper.selectList(tlCarQueryWrapper);
            Map<String, TlCar> carTrackMap = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(Collectors.toList(), tlCars -> tlCars.get(0))));


            List<TlCar> tlCarList1 = tlCarMapper.selectList(new QueryWrapper<>());
            Map<String, TlCar> carMap = tlCarList1.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(toList(), tlCars -> tlCars.get(0))));

            Map<String, String> status = new HashMap<>();
            status.put("status", "1");
            List<TreeEntity> treeList = sysDepartMapper.getTreeList();
            List<TreeEntity> carTree = tlCarMapper.getCarTree(status);
            for (TreeEntity treeEntity : carTree) {
                TlCar tlCar = carTrackMap.get(treeEntity.getId());
                TlCar tlCar1 = carMap.get(treeEntity.getId());
                if (null != tlCar1) {
                    treeEntity.setCarTypeId(tlCar1.getCartypeid());
                }
                if (null != tlCar) {
                    treeEntity.setShiftStatus(1);
                } else {
                    treeEntity.setShiftStatus(0);
                }
            }
            treeList.addAll(carTree);
            trees = TreeUtil.RecursiveAddress(treeList);
        }

        return trees;
    }

    @Override
    public void copyGrid() {
        /** 复刻网格信息 **/
        QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<>();
        List<Grid> gridList = gridMapper.selectList(gridQueryWrapper);
        gridList.forEach(grid -> {
            System.out.println("网格复刻开始: =======================");
            GridSnapshot gridSnapshot = new GridSnapshot();
            gridSnapshot.setId(StringUtils.uuid());
            gridSnapshot.setDate(StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
            BeanUtils.copyProperties(grid, gridSnapshot);
            gridSnapshot.setCreateTime(new Date());
            gridSnapshot.setUpdateTime(new Date());
            gridSnapshotMapper.insert(gridSnapshot);
            System.out.println("网格复刻结束: =======================");
        });
    }

    @Override
    public void updateRptCarWorkByTimeConsuming(String date) {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), grids -> grids.get(0))));

        Jobset jobset = jobsetMapper.selectById(3);
        Double kmOkThreshold = 0d;
        if (null != jobset) {
            kmOkThreshold = jobset.getKmOkThreshold();
        }

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, date);
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
//        carTrackQueryWrapper.lambda().eq(CarTrack::getCarId, "63b6bab0233345ceb3115573423bfc82");
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 2);
//                .or().eq(CarTrack::getJobType, 4);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, date);
//        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, "63b6bab0233345ceb3115573423bfc82");
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
//                .or().eq(RptCarWork::getWorkType, 4);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(t -> {
            t.setWorkTime(BigDecimal.ZERO);
            t.setWorkMileage(BigDecimal.ZERO);
            t.setWorkOil(BigDecimal.ZERO);

            t.setOnGuardMileage(BigDecimal.ZERO);
            t.setOnGuardOil(BigDecimal.ZERO);
            t.setOnGuardTime(BigDecimal.ZERO);
        });
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));

        int startIndexOnGuard, endIndexOnGuard, startIndexWork, endIndexWork;

        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carId);
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    startIndexOnGuard = endIndexOnGuard = startIndexWork = endIndexWork = -1;

                    if (null != gridMap.get(rptCarWork.getGridId())) {
                        if (null != carTrackList) {
                            for (int i = 0; i < carTrackList.size(); i++) {

                                if (carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanStartTime()) >= 0 && carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanEndTime()) <= 0 && rptCarWork.getStatus() == 0) {
                                    rptCarWork.setStatus(1);//进行中：当日排班进行中；
                                }
                                boolean isInGrid = DistanceUtils.insideGrid(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), gridMap.get(rptCarWork.getGridId()));
                                if (isInGrid && rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                        rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {
                                    if (i > 0) {
                                        Long timeOnGuard = (carTrackList.get(i).getTimestamp().getTime() - carTrackList.get(i - 1).getTimestamp().getTime()) / 1000;
                                        rptCarWork.setOnGuardTime(rptCarWork.getOnGuardTime().add(new BigDecimal(timeOnGuard)));
                                        rptCarWork.setOnGuardMileage(rptCarWork.getOnGuardMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));
                                    }


                                    if (startIndexOnGuard == -1) {
                                        startIndexOnGuard = i;
                                    }
                                    endIndexOnGuard = i;

                                    if (null != carTrackList.get(i).getStatus() && carTrackList.get(i).getStatus() == 1) {
                                        //开启喷头总进度   1  //喷头开启 暂时去掉   && currentCarTrack.getStatus() == 1
                                        if (startIndexWork == -1) {
                                            startIndexWork = i;
                                        }
                                        endIndexWork = i;
                                        if (i > 0) {
                                            Long time = (carTrackList.get(i).getTimestamp().getTime() - carTrackList.get(i - 1).getTimestamp().getTime()) / 1000;

                                            rptCarWork.setWorkTime(rptCarWork.getWorkTime().add(new BigDecimal(time)));

                                            rptCarWork.setWorkMileage(rptCarWork.getWorkMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));

                                            if (null != rptCarWork.getWorkMileage() && rptCarWork.getWorkMileage().compareTo(BigDecimal.ZERO) > 0) {
                                                Integer nowProgress = rptCarWork.getWorkMileage().intValue();
                                                Integer allProgress = null == rptCarWork.getAllProgress() ? 0 : rptCarWork.getAllProgress();
                                                if (nowProgress > allProgress) {
                                                    nowProgress = allProgress;
                                                }
                                                Integer completePercent = nowProgress * 100 / allProgress;
                                                rptCarWork.setCompletePercent(completePercent);
                                                // 完成状态
                                                if (carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanEndTime()) <= 0) {
                                                    if (completePercent >= kmOkThreshold) {
                                                        rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                                                    }
                                                } else {
                                                    if (completePercent < kmOkThreshold) {
                                                        rptCarWork.setStatus(2);//未完成：考勤时间结束，未完成作业；
                                                    } else {
                                                        rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                                                    }
                                                }

                                            } else {
                                                rptCarWork.setStatus(0);
                                            }
                                        }
                                        if (null == rptCarWork.getWorkStartTime() || "".equals(rptCarWork.getWorkStartTime())) {
                                            rptCarWork.setWorkStartTime(carTrackList.get(i).getTimestamp());
                                        }
                                        rptCarWork.setWorkEndTime(carTrackList.get(i).getTimestamp());


                                    } else {
                                        addOil(startIndexWork, endIndexWork, carTrackList, rptCarWork, 1);
                                        startIndexWork = -1;
                                        endIndexWork = -1;
                                    }

                                } else {
                                    //作业油耗
                                    addOil(startIndexOnGuard, endIndexOnGuard, carTrackList, rptCarWork, 2);
                                    startIndexOnGuard = -1;
                                    endIndexOnGuard = -1;
                                    addOil(startIndexWork, endIndexWork, carTrackList, rptCarWork, 1);
                                    startIndexWork = -1;
                                    endIndexWork = -1;
                                }

                            }
                            //作业油耗
                            addOil(startIndexOnGuard, endIndexOnGuard, carTrackList, rptCarWork, 2);
                            addOil(startIndexWork, endIndexWork, carTrackList, rptCarWork, 1);
                        }
                    }
                }
            }

        }
        rptCarWorkMap.keySet().forEach(t -> {
            rptCarWorkMap.get(t).forEach(t1 -> {
                rptCarWorkMapper.updateById(t1);
                System.out.println("开始更新作业油耗 作业里程 作业耗时 ===========================");
            });
        });
    }

    private void addOil(int startIndex, int endIndex, List<CarTrack> carTrackList, RptCarWork rptCarWork, int type) {

        // type = 1 作业油耗
        // type = 2 在岗油耗  可能会比 作业油耗小  因为 前后回头负数  导致的
        if (startIndex != -1 && endIndex != -1) {
//            if (startIndex != endIndex && rptCarWork.getId().equals("94176bc0c205474c8bcffbf3a7ceeaf2")){
//                System.out.println("----------------" + startIndex + "-" + endIndex + "#" + type);
//            }
            int j = startIndex;
            while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                j++;
            }
            startIndex = j;
            j = endIndex;
            while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                j--;
            }
            endIndex = j;
            if (startIndex < endIndex) {
                BigDecimal addOilNum = BigDecimal.ZERO;
                for (j = startIndex + 1; j <= endIndex; j++) {
                    addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                }
                BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                        .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                        .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                if (type == 1) {
                    rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                } else if (type == 2) {
                    rptCarWork.setOnGuardOil(rptCarWork.getOnGuardOil().add(oilWear));
                }
            }
//            if (startIndex != endIndex && rptCarWork.getId().equals("94176bc0c205474c8bcffbf3a7ceeaf2")) {
//                System.out.println("=======================" + rptCarWork.getWorkOil() + "-" + rptCarWork.getOnGuardOil());
//            }
        }
    }

    @Override
    public void updateRptCarWorkByGarbageTransitWork(String date) {

        Jobset jobset = jobsetMapper.selectById(5);

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, date);
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 4);
//                .or().eq(CarTrack::getJobType, 4);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, date);
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 4);
//                .or().eq(RptCarWork::getWorkType, 4);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(t -> {
            t.setWorkTime(BigDecimal.ZERO);
            t.setWorkMileage(BigDecimal.ZERO);
            t.setWorkOil(BigDecimal.ZERO);
        });
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));

        int startIndex;
        int endIndex;
        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carId);
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    startIndex = -1;
                    endIndex = -1;
                    if (null != carTrackList) {
                        for (int i = 0; i < carTrackList.size(); i++) {
                            if (carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanStartTime()) >= 0 && carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanEndTime()) <= 0 && rptCarWork.getStatus() == 0) {
                                rptCarWork.setStatus(1);//进行中：当日排班进行中；
                            }
                            if (rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                    rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {
                                if (startIndex == -1) {
                                    startIndex = i;
                                }
                                endIndex = i;
                                if (i > 0) {
                                    Long time = (carTrackList.get(i).getTimestamp().getTime() - carTrackList.get(i - 1).getTimestamp().getTime()) / 1000;
                                    rptCarWork.setWorkTime(rptCarWork.getWorkTime().add(new BigDecimal(time)));

                                    rptCarWork.setWorkMileage(rptCarWork.getWorkMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));

                                    if (null != rptCarWork.getWorkMileage() && rptCarWork.getWorkMileage().compareTo(BigDecimal.ZERO) > 0) {
                                        BigDecimal mileage = rptCarWork.getWorkMileage();
                                        BigDecimal expectMileage = rptCarWork.getExpectMileage().multiply(new BigDecimal(1000));
                                        BigDecimal completePercent = (mileage.multiply(new BigDecimal(100)).divide(expectMileage, 4, BigDecimal.ROUND_HALF_UP)).setScale(0, BigDecimal.ROUND_UP);
                                        rptCarWork.setCompletePercent(completePercent.intValue());
                                        // 完成状态
                                        if (carTrackList.get(i).getTimestamp().compareTo(rptCarWork.getPlanEndTime()) <= 0) {

                                            if (completePercent.compareTo(new BigDecimal(jobset.getPointOkThreshold())) >= 0) {
                                                rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                                            }
                                        } else {
                                            if (completePercent.compareTo(new BigDecimal(jobset.getPointOkThreshold())) < 0) {
                                                rptCarWork.setStatus(2);//未完成：考勤时间结束，未完成作业；
                                            } else {
                                                rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                                            }
                                        }

                                    } else {
                                        rptCarWork.setStatus(0);
                                    }
                                }

                            } else {
                                //作业油耗
                                if (startIndex != -1 && endIndex != -1) {
                                    int j = startIndex;
                                    while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                        j++;
                                    }
                                    startIndex = j;
                                    j = endIndex;
                                    while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                        j--;
                                    }
                                    endIndex = j;
                                    if (startIndex < endIndex) {
                                        BigDecimal addOilNum = BigDecimal.ZERO;
                                        for (j = startIndex + 1; j <= endIndex; j++) {
                                            addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                        }
                                        BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                                .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                                .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                        rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                                    }
                                }
                                startIndex = -1;
                                endIndex = -1;
                            }

                        }
                        //作业油耗
                        if (startIndex != -1 && endIndex != -1) {
                            int j = startIndex;
                            while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                j++;
                            }
                            startIndex = j;
                            j = endIndex;
                            while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                j--;
                            }
                            endIndex = j;
                            if (startIndex < endIndex) {
                                BigDecimal addOilNum = BigDecimal.ZERO;
                                for (j = startIndex + 1; j <= endIndex; j++) {
                                    addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                }
                                BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                        .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                        .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                            }
                            startIndex = -1;
                            endIndex = -1;
                        }
                    }
                }
            }

        }
        rptCarWorkMap.keySet().forEach(t -> {
            rptCarWorkMap.get(t).forEach(t1 -> {
                rptCarWorkMapper.updateById(t1);
            });
        });
    }

    @Override
    public void updateRptCarWorkByTimeConsuming(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), grids -> grids.get(0))));

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().between(CarTrack::getWorkDay, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarId())) {
            carTrackQueryWrapper.lambda().eq(CarTrack::getCarId, carWorkPlanCountTo.getCarId());
        }
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 2);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        }
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(t -> {
            t.setWorkTime(BigDecimal.ZERO);
            t.setWorkMileage(BigDecimal.ZERO);
            t.setWorkOil(BigDecimal.ZERO);
        });
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        int startIndex;
        int endIndex;
        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carId);
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    startIndex = -1;
                    endIndex = -1;
                    if (null != gridMap.get(rptCarWork.getGridId())) {
                        if (null != carTrackList) {
                            for (int i = 0; i < carTrackList.size(); i++) {
                                boolean isInGrid = DistanceUtils.insideGrid(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), gridMap.get(rptCarWork.getGridId()));
                                if (isInGrid && rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                        rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {
                                    if (startIndex == -1) {
                                        startIndex = i;
                                    }
                                    endIndex = i;
                                    if (i > 0) {
                                        //开启喷头总进度   1  //喷头开启 暂时去掉   && currentCarTrack.getStatus() == 1
                                        if (null != carTrackList.get(i).getStatus() && carTrackList.get(i).getStatus() == 1) {
                                            rptCarWork.setWorkMileage(rptCarWork.getWorkMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));
                                        }
                                        Long time = (carTrackList.get(i).getTimestamp().getTime() - carTrackList.get(i - 1).getTimestamp().getTime()) / 1000;
                                        rptCarWork.setWorkTime(rptCarWork.getWorkTime().add(new BigDecimal(time)));
                                        rptCarWork.setWorkMileage(rptCarWork.getWorkMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));
                                    }

                                    if (null == rptCarWork.getWorkStartTime() || "".equals(rptCarWork.getWorkStartTime())) {
                                        rptCarWork.setWorkStartTime(carTrackList.get(i).getTimestamp());
                                    }
                                    rptCarWork.setWorkEndTime(carTrackList.get(i).getTimestamp());

                                } else {
                                    //作业油耗
                                    if (startIndex != -1 && endIndex != -1) {
                                        int j = startIndex;
                                        while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                            j++;
                                        }
                                        startIndex = j;
                                        j = endIndex;
                                        while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                            j--;
                                        }
                                        endIndex = j;
                                        if (startIndex < endIndex) {
                                            BigDecimal addOilNum = BigDecimal.ZERO;
                                            for (j = startIndex + 1; j <= endIndex; j++) {
                                                addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                            }
                                            BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                                    .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                                    .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                            rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                                        }
                                    }
                                    startIndex = -1;
                                    endIndex = -1;
                                }

                            }
                            //作业油耗
                            if (startIndex != -1 && endIndex != -1) {
                                int j = startIndex;
                                while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                    j++;
                                }
                                startIndex = j;
                                j = endIndex;
                                while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                    j--;
                                }
                                endIndex = j;
                                if (startIndex < endIndex) {
                                    BigDecimal addOilNum = BigDecimal.ZERO;
                                    for (j = startIndex + 1; j <= endIndex; j++) {
                                        addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                    }
                                    BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                            .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                            .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                    rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                                }
                                startIndex = -1;
                                endIndex = -1;
                            }
                        }
                    }
                }
            }

        }
        rptCarWorkMap.keySet().forEach(t -> {
            rptCarWorkMap.get(t).forEach(t1 -> {
                rptCarWorkMapper.updateById(t1);
            });
        });
    }

    @Override
    public void updateRptCarWorkByTimeConsuming2(CarWorkPlanCountTo carWorkPlanCountTo) {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), grids -> grids.get(0))));

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().between(CarTrack::getWorkDay, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarId())) {
            carTrackQueryWrapper.lambda().eq(CarTrack::getCarId, carWorkPlanCountTo.getCarId());
        }
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 3);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        if (StringUtils.isNotEmpty(carWorkPlanCountTo.getCarId())) {
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carWorkPlanCountTo.getCarId());
        }
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(t -> {
            t.setWorkTime(BigDecimal.ZERO);
            t.setWorkMileage(BigDecimal.ZERO);
            t.setWorkOil(BigDecimal.ZERO);
        });
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        int startIndex;
        int endIndex;
        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carId);
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    startIndex = -1;
                    endIndex = -1;
                    if (null != gridMap.get(rptCarWork.getGridId())) {
                        if (null != carTrackList) {
                            for (int i = 0; i < carTrackList.size(); i++) {
                                boolean isInGrid = DistanceUtils.insideGrid(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), gridMap.get(rptCarWork.getGridId()));
                                if (isInGrid && rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                        rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {
                                    if (startIndex == -1) {
                                        startIndex = i;
                                    }
                                    endIndex = i;
                                    if (i > 0) {
                                        Long time = (carTrackList.get(i).getTimestamp().getTime() - carTrackList.get(i - 1).getTimestamp().getTime()) / 1000;
                                        rptCarWork.setWorkTime(rptCarWork.getWorkTime().add(new BigDecimal(time)));
                                        rptCarWork.setWorkMileage(rptCarWork.getWorkMileage().add(new BigDecimal(carTrackList.get(i).getDistanceBetween())));
                                    }
                                    rptCarWork.setWorkStartTime(carTrackList.get(0).getTimestamp());
                                    rptCarWork.setWorkEndTime(carTrackList.get(carTrackList.size() - 1).getTimestamp());


                                } else {
                                    //作业油耗
                                    if (startIndex != -1 && endIndex != -1) {
                                        int j = startIndex;
                                        while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                            j++;
                                        }
                                        startIndex = j;
                                        j = endIndex;
                                        while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                            j--;
                                        }
                                        endIndex = j;
                                        if (startIndex < endIndex) {
                                            BigDecimal addOilNum = BigDecimal.ZERO;
                                            for (j = startIndex + 1; j <= endIndex; j++) {
                                                addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                            }
                                            BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                                    .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                                    .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                            rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                                        }
                                    }
                                    startIndex = -1;
                                    endIndex = -1;
                                }

                            }
                            //作业油耗
                            if (startIndex != -1 && endIndex != -1) {
                                int j = startIndex;
                                while (j < endIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                    j++;
                                }
                                startIndex = j;
                                j = endIndex;
                                while (j > startIndex && carTrackList.get(j).getOilMass().compareTo(BigDecimal.ZERO) == 0) {
                                    j--;
                                }
                                endIndex = j;
                                if (startIndex < endIndex) {
                                    BigDecimal addOilNum = BigDecimal.ZERO;
                                    for (j = startIndex + 1; j <= endIndex; j++) {
                                        addOilNum = addOilNum.add(null == carTrackList.get(j).getAddOilNum() ? BigDecimal.ZERO : new BigDecimal(carTrackList.get(j).getAddOilNum()));
                                    }
                                    BigDecimal oilWear = MathUtils.getBigDecimal(carTrackList.get(startIndex).getOilMass())
                                            .subtract(MathUtils.getBigDecimal(carTrackList.get(endIndex).getOilMass()))
                                            .add(addOilNum).setScale(2, BigDecimal.ROUND_HALF_UP);
                                    rptCarWork.setWorkOil(rptCarWork.getWorkOil().add(oilWear));
                                }
                                startIndex = -1;
                                endIndex = -1;
                            }
                        }
                    }
                }
            }

        }
        rptCarWorkMap.keySet().forEach(t -> {
            rptCarWorkMap.get(t).forEach(t1 -> {
                rptCarWorkMapper.updateById(t1);
            });
        });
    }

    @Override
    public void updateRptCarWorkByTimeConsuming1() {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), grids -> grids.get(0))));

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, "2021-12-11");
//        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, "2021-12-11");
//        carTrackQueryWrapper.lambda().eq(CarTrack::getCarId, "63b6bab0233345ceb3115573423bfc82");
        carTrackQueryWrapper.lambda().orderByDesc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, "2021-12-11");
//        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, "2021-12-11");
//        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, "63b6bab0233345ceb3115573423bfc82");
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        rptCarWorkList.forEach(t -> {
            t.setWorkTime(BigDecimal.ZERO);
        });
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            if (null != carTrackList) {
                for (int i = 1; i < carTrackList.size(); i++) {
                    List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carTrackList.get(i).getCarId());
                    if (null != rptCarWorkList1) {
                        for (RptCarWork rptCarWork : rptCarWorkList1) {
                            if (null != gridMap.get(rptCarWork.getGridId())) {
                                boolean isInGrid = DistanceUtils.insideGrid(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), gridMap.get(rptCarWork.getGridId()));
                                if (isInGrid) {
                                    if (rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                            rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {

                                        Long time = (carTrackList.get(i - 1).getTimestamp().getTime() - carTrackList.get(i).getTimestamp().getTime()) / 1000;
                                        rptCarWork.setWorkTime(rptCarWork.getWorkTime().add(new BigDecimal(time)));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        rptCarWorkMap.keySet().forEach(t -> {
            rptCarWorkMap.get(t).forEach(t1 -> {
                rptCarWorkMapper.updateById(t1);
            });
        });
    }

    @Override
    public SsCarPlanVo ssCarPlanWork() {

        Date date = new Date();
        String now = DateUtil.formatDate(date);
        // 作业类型：2机械清扫；3垃圾清运；4垃圾转运
        // 状态：0未开始；1进行中；2已结束
        Map<Object, Object> jxqsMap = new HashMap<>();
        jxqsMap.put("workType", 2);
        jxqsMap.put("workDate", now);
        List<RptCarWork> jxqsResultList = rptCarWorkMapper.listByPlan(jxqsMap);

        Integer totalCount = (int) jxqsResultList.stream().filter(t -> null != t.getWorkTypeName() && t.getWorkTypeName().equals("洒水作业")).count();

        Integer waitingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 0 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("洒水作业"))).count();
        Integer workingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 1 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("洒水作业"))).count();
        Integer unFinishCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 2 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("洒水作业"))).count();
        Integer finishCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 3 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("洒水作业"))).count();
        System.out.println("=====================");
        // 洒水
        int totalCountSs = (int) jxqsResultList.stream().filter(t -> "洒水作业".equals(t.getWorkTypeName())).count();
        int finishCountSs = (int) jxqsResultList.stream().filter(t -> "洒水作业".equals(t.getWorkTypeName())).filter(t1 -> t1.getAllStatus() == 3).count();
        BigDecimal finishRateSs = (finishCountSs == 0) ? BigDecimal.ZERO : (new BigDecimal(finishCountSs).divide(new BigDecimal(totalCountSs), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));

        SsCarPlanVo ssCarPlanVo = new SsCarPlanVo();
        ssCarPlanVo.setTotalCount(null == totalCount ? 0 : totalCount);
        ssCarPlanVo.setFinishCount(null == finishCount ? 0 : finishCount);
        ssCarPlanVo.setWorkingCount(null == workingCount ? 0 : workingCount);
        ssCarPlanVo.setWaitingCount(null == waitingCount ? 0 : waitingCount);
        ssCarPlanVo.setUnFinishCount(null == unFinishCount ? 0 : unFinishCount);
        ssCarPlanVo.setFinishRateSs(finishRateSs);

        return ssCarPlanVo;
    }

    @Override
    public QsCarPlanVo qsCarPlanWork() {

        Date date = new Date();
        String now = DateUtil.formatDate(date);
        // 作业类型：2机械清扫；3垃圾清运；4垃圾转运
        // 状态：0未开始；1进行中；2已结束
        Map<Object, Object> jxqsMap = new HashMap<>();
        jxqsMap.put("workType", 2);
        jxqsMap.put("workDate", now);
        List<RptCarWork> jxqsResultList = rptCarWorkMapper.listByPlan(jxqsMap);

        Integer totalCount = (int) jxqsResultList.stream().filter(t -> null != t.getWorkTypeName() && t.getWorkTypeName().equals("清扫作业")).count();
        Integer waitingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 0 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("清扫作业"))).count();
        Integer workingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 1 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("清扫作业"))).count();
        Integer unFinishCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 2 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("清扫作业"))).count();
        Integer finishCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 3 && (null != t.getWorkTypeName() && t.getWorkTypeName().equals("清扫作业"))).count();
        System.out.println("=====================");
        // 清掃
        int totalCountQs = (int) jxqsResultList.stream().filter(t -> "清扫作业".equals(t.getWorkTypeName())).count();
        int finishCountQs = (int) jxqsResultList.stream().filter(t -> "清扫作业".equals(t.getWorkTypeName())).filter(t1 -> t1.getAllStatus() == 3).count();
        BigDecimal finishRateQs = (finishCountQs == 0) ? BigDecimal.ZERO : (new BigDecimal(finishCountQs).divide(new BigDecimal(totalCountQs), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));


        QsCarPlanVo qsCarPlanVo = new QsCarPlanVo();
        qsCarPlanVo.setTotalCount(null == totalCount ? 0 : totalCount);
        qsCarPlanVo.setUnFinishCount(null == unFinishCount ? 0 : unFinishCount);
        qsCarPlanVo.setFinishCount(null == finishCount ? 0 : finishCount);
        qsCarPlanVo.setWorkingCount(null == workingCount ? 0 : workingCount);
        qsCarPlanVo.setWaitingCount(null == waitingCount ? 0 : waitingCount);
        qsCarPlanVo.setFinishRateQs(finishRateQs);

        return qsCarPlanVo;
    }

    @Override
    public List<JxqsKaoQin> ssCarPlanWorkList(JSONObject params) {

        List<JxqsKaoQin> jxqsKaoQinList = new ArrayList();
        QueryWrapper<WorkKaoqin> workKaoqinQueryWrapper = new QueryWrapper<>();
        String date = DateUtil.formatDate(new Date());
        if (StringUtils.isNotEmpty(date)) {
            params.put("startDate", StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
            params.put("endDate", StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        }
        String planName = params.getString("planName");

        List<WorkKaoqin> workKaoqinList = workKaoqinMapper.selectList(workKaoqinQueryWrapper);
        //关联查询排班和网格
        List<RptCarWork> rptCarWorkList = bigScreenMapper.getJxqsWorkSceneList(params);
        //关联查询人员和车辆
        List<RptCarWork> jxzyWorkDetailList = bigScreenMapper.jxzyWorkDetail(params);
        Jobset jobset = jobsetMapper.selectById("3");
        for (RptCarWork rptCarWork : rptCarWorkList) {
            if (rptCarWork.getWorkTypeName().equals("洒水作业")) {
                JxqsKaoQin jxqsKaoQin = new JxqsKaoQin();
                jxqsKaoQin.setJxzyWorkDetaiFilter(new ArrayList<>());
                jxqsKaoQin.setKaoQinId(rptCarWork.getShiftId());

                jxqsKaoQin.setRptCarWork(rptCarWork);
                List<WorkKaoqin> workKaoqinListFilter = workKaoqinList.stream().filter(t -> t.getWorkKaoqinId().equals(rptCarWork.getShiftId())).collect(Collectors.toList());
                if (workKaoqinListFilter.size() > 0) {
                    jxqsKaoQin.setWorkKaoqin(workKaoqinListFilter.get(0));
                }
                List<RptCarWork> list1 = jxzyWorkDetailList.stream().filter(t -> t.getPlanId() != null && t.getPlanId().equals(rptCarWork.getPlanId())).collect(Collectors.toList());
                List<RptCarWork> list2 = list1.stream().filter(t -> t.getShiftId().equals(rptCarWork.getShiftId())).collect(Collectors.toList());
                int totalTangci = 0;
                for (int i = 0; i < list2.size(); i++) {
                    RptCarWork t = list2.get(i);

                    // 把车推进去
                    // 单程距离
                    //总里程  除以 趟次  每趟里程 //2000
                    BigDecimal singleProgress = (t.getTangCi() == null || t.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(t.getAllProgress()).divide(new BigDecimal(t.getTangCi()), BigDecimal.ROUND_HALF_UP);
                    // 单程需完成距离
                    if (null != jobset) {
                        //每趟里程  乘以 里程完成率阀值 %  // 2000 * 0.7  1400
                        BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);
                        // 现里程  除以    requireSingleProgress  得到完成了几趟  4000/1400

                        int thisTangci = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == t.getWorkMileage() ? BigDecimal.ZERO : t.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();
                        if (null == rptCarWork.getTangCi()) {
                            rptCarWork.setTangCi(0);
                        }
                        if (thisTangci > rptCarWork.getTangCi()) {
                            thisTangci = rptCarWork.getTangCi();
                        }
                        t.setFinishTangci(thisTangci);
                        jxqsKaoQin.getJxzyWorkDetaiFilter().add(t);
                        // 计算总完成趟次
                        totalTangci += thisTangci;
                    }

                    int rate = 0;
                    if (0 != rptCarWork.getTangCi()) {
                        rate = rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0 ? 0 : totalTangci * 100 / rptCarWork.getTangCi();
//                rate = getRandomNumberInRange(1, 100);
                        if (rate >= 100) {
                            rate = 100;
                        }
                    }
                    jxqsKaoQin.setRate(rate);
                    jxqsKaoQinList.add(jxqsKaoQin);

                }
            }

        }
        //根据完成度排序
        Collections.sort(jxqsKaoQinList, new Comparator<JxqsKaoQin>() {
            public int compare(JxqsKaoQin o1, JxqsKaoQin o2) {
                Integer rate1 = Integer.valueOf(o1.getRate());
                Integer rate2 = Integer.valueOf(o2.getRate());
                return rate2.compareTo(rate1);
            }
        });
        if (StringUtils.isNotEmpty(planName)) {
            List<JxqsKaoQin> jxqsKaoQinList1 = jxqsKaoQinList.stream().filter(jxqsKaoQin -> null != jxqsKaoQin.getRptCarWork() && null != jxqsKaoQin.getRptCarWork().getPlanName() && jxqsKaoQin.getRptCarWork().getPlanName().contains(planName)).collect(toList());
            return jxqsKaoQinList1;
        }
        //驾驶员姓名  车牌号  车辆类型 网格名称 车辆id
        return jxqsKaoQinList;
    }

    @Override
    public List<JxqsKaoQin> qsCarPlanWorkList(JSONObject params) {

        List<JxqsKaoQin> jxqsKaoQinList = new ArrayList();
        QueryWrapper<WorkKaoqin> workKaoqinQueryWrapper = new QueryWrapper<>();
        String date = DateUtil.formatDate(new Date());
        if (StringUtils.isNotEmpty(date)) {
            params.put("startDate", StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
            params.put("endDate", StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        }
        String planName = params.getString("planName");
        List<WorkKaoqin> workKaoqinList = workKaoqinMapper.selectList(workKaoqinQueryWrapper);
        //关联查询排班和网格
        List<RptCarWork> rptCarWorkList = bigScreenMapper.getJxqsWorkSceneList(params);
        //关联查询人员和车辆
        List<RptCarWork> jxzyWorkDetailList = bigScreenMapper.jxzyWorkDetail(params);
        Jobset jobset = jobsetMapper.selectById("3");
        for (RptCarWork rptCarWork : rptCarWorkList) {
            if (rptCarWork.getWorkTypeName().equals("清扫作业")) {
                JxqsKaoQin jxqsKaoQin = new JxqsKaoQin();
                jxqsKaoQin.setJxzyWorkDetaiFilter(new ArrayList<>());
                jxqsKaoQin.setKaoQinId(rptCarWork.getShiftId());
                jxqsKaoQin.setRptCarWork(rptCarWork);
                List<WorkKaoqin> workKaoqinListFilter = workKaoqinList.stream().filter(t -> t.getWorkKaoqinId().equals(rptCarWork.getShiftId())).collect(Collectors.toList());
                if (workKaoqinListFilter.size() > 0) {
                    jxqsKaoQin.setWorkKaoqin(workKaoqinListFilter.get(0));
                }
                List<RptCarWork> list1 = jxzyWorkDetailList.stream().filter(t -> t.getPlanId() != null && t.getPlanId().equals(rptCarWork.getPlanId())).collect(Collectors.toList());
                List<RptCarWork> list2 = list1.stream().filter(t -> t.getShiftId().equals(rptCarWork.getShiftId())).collect(Collectors.toList());
                int totalTangci = 0;
                for (int i = 0; i < list2.size(); i++) {
                    RptCarWork t = list2.get(i);
                    // 把车推进去
                    // 单程距离
                    //总里程  除以 趟次  每趟里程 //2000
                    BigDecimal singleProgress = (t.getTangCi() == null || t.getTangCi() == 0) ? BigDecimal.ZERO : new BigDecimal(t.getAllProgress()).divide(new BigDecimal(t.getTangCi()), BigDecimal.ROUND_HALF_UP);
                    // 单程需完成距离
                    if (null != jobset) {
                        //每趟里程  乘以 里程完成率阀值 %  // 2000 * 0.7  1400
                        BigDecimal requireSingleProgress = new BigDecimal(jobset.getKmOkThreshold()).multiply(singleProgress).divide(new BigDecimal(100), BigDecimal.ROUND_HALF_UP);
                        // 现里程  除以    requireSingleProgress  得到完成了几趟  4000/1400

                        int thisTangci = (requireSingleProgress == null || requireSingleProgress.equals(BigDecimal.ZERO)) ? 0 : (null == t.getWorkMileage() ? BigDecimal.ZERO : t.getWorkMileage()).divide(requireSingleProgress, BigDecimal.ROUND_FLOOR).intValue();
                        if (null == rptCarWork.getTangCi()) {
                            rptCarWork.setTangCi(0);
                        }
                        if (thisTangci > rptCarWork.getTangCi()) {
                            thisTangci = rptCarWork.getTangCi();
                        }
                        t.setFinishTangci(thisTangci);
                        jxqsKaoQin.getJxzyWorkDetaiFilter().add(t);
                        // 计算总完成趟次
                        totalTangci += thisTangci;
                    }
                }
                int rate = 0;
                if (0 != rptCarWork.getTangCi()) {
                    rate = rptCarWork.getTangCi() == null || rptCarWork.getTangCi() == 0 ? 0 : totalTangci * 100 / rptCarWork.getTangCi();
//                rate = getRandomNumberInRange(1, 100);
                    if (rate >= 100) {
                        rate = 100;
                    }
                }
                jxqsKaoQin.setRate(rate);
                jxqsKaoQinList.add(jxqsKaoQin);
            }
        }
        //根据完成度排序
        Collections.sort(jxqsKaoQinList, new Comparator<JxqsKaoQin>() {
            public int compare(JxqsKaoQin o1, JxqsKaoQin o2) {
                Integer rate1 = Integer.valueOf(o1.getRate());
                Integer rate2 = Integer.valueOf(o2.getRate());
                return rate2.compareTo(rate1);
            }
        });
        //驾驶员姓名  车牌号  车辆类型 网格名称 车辆id
        if (StringUtils.isNotEmpty(planName)) {
            List<JxqsKaoQin> jxqsKaoQinList1 = jxqsKaoQinList.stream().filter(jxqsKaoQin -> null != jxqsKaoQin.getRptCarWork() && null != jxqsKaoQin.getRptCarWork().getPlanName() && jxqsKaoQin.getRptCarWork().getPlanName().contains(planName)).collect(toList());
            return jxqsKaoQinList1;
        }
        return jxqsKaoQinList;
    }

    @Override
    public void updateCleanType(String date) {

        QueryWrapper<RptCarWorkFacility> rptCarWorkFacilityQueryWrapper = new QueryWrapper<>();
        rptCarWorkFacilityQueryWrapper.lambda().eq(RptCarWorkFacility::getWorkDate, date);
        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(rptCarWorkFacilityQueryWrapper);
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityListMap = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));


        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, date);
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 3);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrapper);

//        Map<String, List<CarTrack>> carTrackMap = carTrackList.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, date);
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        for (CarTrack carTrack : carTrackList) {
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carTrack.getCarId());
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityListMap.get(rptCarWork.getId());
                    if (null != rptCarWorkFacilityList1) {
                        for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                            TlFacilities tlFacilities = tlFacilitiesMap.get(rptCarWorkFacility.getCollectPointId());
                            if (null != tlFacilities) {
                                String[] faclAddr = tlFacilities.getFacladdr().split(",");
                                String lngAddr = faclAddr[0];
                                String latAddr = faclAddr[1];
                                Boolean flag = DistanceUtils.insideCircle(Double.parseDouble(carTrack.getLng()), Double.parseDouble(carTrack.getLat()), Double.parseDouble(lngAddr), Double.parseDouble(latAddr), Double.parseDouble(tlFacilities.getRadii().toString()));
                                if (flag) {
                                    rptCarWorkFacility.setCarTrackId(carTrack.getId());
                                    rptCarWorkFacility.setCleanType(1);
                                    rptCarWorkFacilityMapper.updateById(rptCarWorkFacility);
                                }
                            }
                        }
                    }
                }
            }
        }

    }

    @Override
    public void updateTrashPickupWork(String date) {

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, date);
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 3);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, date);
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));

        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            if (null != carTrackList) {
                for (int i = 1; i < carTrackList.size(); i++) {
                    List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carTrackList.get(i).getCarId());
                    if (null != rptCarWorkList1) {
                        for (RptCarWork rptCarWork : rptCarWorkList1) {
                            rptCarWork.setWorkStartTime(carTrackList.get(0).getTimestamp());
                            rptCarWork.setWorkEndTime(carTrackList.get(carTrackList.size() - 1).getTimestamp());
                            rptCarWorkMapper.updateById(rptCarWork);
                        }
                    }
                }
            }
        }

    }

    @Override
    public void updateStand(String date) {
        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, date);
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
//        carTrackQueryWrapper.lambda().eq(CarTrack::getCarId, "2f47c145cadf49e9b1a11a4463286bf6");
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 4);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList1 = carTrackMapper.selectList(carTrackQueryWrapper);
        Map<String, List<CarTrack>> carTrackMap = carTrackList1.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, date);
//        carWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, "2f47c145cadf49e9b1a11a4463286bf6");
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 4);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(new QueryWrapper<>());
        Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap = carPlanCollectStandList.stream().collect(Collectors.groupingBy(CarPlanCollectStand::getCarPlanId));

        List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(new QueryWrapper<>());
        Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap = carPlanProcessStandList.stream().collect(Collectors.groupingBy(CarPlanProcessStand::getCarPlanId));

        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(new QueryWrapper<>());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter2 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 2).collect(toList());
        List<RptCarWorkFacility> rptCarWorkFacilityFilter3 = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getWorkType() && rptCarWorkFacility.getWorkType() == 3).collect(toList());
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap2 = rptCarWorkFacilityFilter2.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getCollectStandId()).collect(Collectors.groupingBy(RptCarWorkFacility::getCollectStandId));
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityMap3 = rptCarWorkFacilityFilter3.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getProcessStandId()).collect(Collectors.groupingBy(RptCarWorkFacility::getProcessStandId));


        for (String carId : carTrackMap.keySet()) {
            List<CarTrack> carTrackList = carTrackMap.get(carId);
            if (null != carTrackList) {
                for (int i = 0; i < carTrackList.size(); i++) {
                    List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carTrackList.get(i).getCarId());
                    if (null != rptCarWorkList1) {
                        Map<String, List<RptCarWork>> rptCarWorkList1Map = rptCarWorkList1.stream().collect(Collectors.groupingBy(RptCarWork::getShiftId));
                        for (String shiftId : rptCarWorkList1Map.keySet()) {
                            List<RptCarWork> rptCarWorkList2 = rptCarWorkList1Map.get(shiftId);
                            if (null != rptCarWorkList2) {
                                for (RptCarWork rptCarWork : rptCarWorkList2) {
                                    CarPlan carPlan = carPlanMapper.selectById(rptCarWork.getPlanId());
                                    if (null != carPlan) {
                                        List<CarPlanCollectStand> carPlanCollectStandList1 = carPlanCollectStandMap.get(carPlan.getId());
                                        if (null != carPlanCollectStandList1) {
                                            for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList1) {
                                                List<RptCarWorkFacility> rptCarWorkFacilityList2 = rptCarWorkFacilityMap2.get(carPlanCollectStand.getCollectStandId());
                                                if (null != rptCarWorkFacilityList2) {
                                                    for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList2) {

                                                        Boolean isInGrid = DistanceUtils.insideCircle(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), Double.parseDouble(rptCarWorkFacility.getLng()), Double.parseDouble(rptCarWorkFacility.getLat()), Double.parseDouble(rptCarWorkFacility.getRadii().toString()));

                                                        if (isInGrid && rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                                                rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {


                                                            RptCarWorkDetail rptCarWorkDetail1 = RedisUtil.get(RptCarWorkDetailLastOneKeyToday.token, rptCarWork.getId() + "_" + rptCarWorkFacility.getId(), RptCarWorkDetail.class);

                                                            String uuid;

                                                            if (null != rptCarWorkDetail1 && null != rptCarWorkDetail1.getEndTime() && ((int) (carTrackList.get(i).getTimestamp().getTime() - rptCarWorkDetail1.getEndTime().getTime()) / 1000) < 7200) {
                                                                uuid = rptCarWorkDetail1.getId();
                                                                RptCarWorkDetail rptCarWorkDetail = new RptCarWorkDetail();
                                                                rptCarWorkDetail.setId(rptCarWorkDetail1.getId());
                                                                rptCarWorkDetail.setEndTime(carTrackList.get(i).getTimestamp());
                                                                rptCarWorkDetailMapper.updateById(rptCarWorkDetail);

                                                            } else {
                                                                RptCarWorkDetail rptCarWorkDetail2 = new RptCarWorkDetail();
                                                                uuid = StringUtils.uuid();
                                                                rptCarWorkDetail2.setId(uuid);
                                                                rptCarWorkDetail2.setRptCarWorkId(rptCarWork.getId());
                                                                rptCarWorkDetail2.setCarTrackId(carTrackList.get(i).getId());
                                                                rptCarWorkDetail2.setRptCarWorkFacilityId(rptCarWorkFacility.getId());
                                                                rptCarWorkDetail2.setCarId(rptCarWork.getCarId());
                                                                rptCarWorkDetail2.setCarNumber(rptCarWork.getCarNum());
                                                                rptCarWorkDetail2.setFacilityName(rptCarWorkFacility.getFacilityName());
                                                                rptCarWorkDetail2.setLat(rptCarWorkFacility.getLat());
                                                                rptCarWorkDetail2.setLng(rptCarWorkFacility.getLng());
                                                                rptCarWorkDetail2.setRadii(rptCarWorkFacility.getRadii());
                                                                rptCarWorkDetail2.setWorkDate(DateUtil.format(new Date(), "yyyy-MM-dd"));
                                                                if (null == rptCarWorkDetail2.getStartTime() || "".equals(rptCarWorkDetail2.getStartTime())) {
                                                                    rptCarWorkDetail2.setStartTime(carTrackList.get(i).getTimestamp());
                                                                }
                                                                rptCarWorkDetail2.setEndTime(carTrackList.get(i).getTimestamp());
                                                                rptCarWorkDetailMapper.insert(rptCarWorkDetail2);
                                                            }

                                                            RptCarWorkDetail rptCarWorkDetail = new RptCarWorkDetail();
                                                            rptCarWorkDetail.setId(uuid);
                                                            rptCarWorkDetail.setEndTime(carTrackList.get(i).getTimestamp());

                                                            RedisUtil.set(RptCarWorkDetailLastOneKeyToday.token, rptCarWork.getId() + "_" + rptCarWorkFacility.getId(), rptCarWorkDetail);

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        List<CarPlanProcessStand> carPlanProcessStandList1 = carPlanProcessStandMap.get(carPlan.getId());
                                        if (null != carPlanProcessStandList1) {
                                            for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList1) {
                                                List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityMap3.get(carPlanProcessStand.getProcessStandId());
                                                if (null != rptCarWorkFacilityList1) {
                                                    for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                                                        Boolean isInGrid = DistanceUtils.insideCircle(Double.parseDouble(carTrackList.get(i).getLng()), Double.parseDouble(carTrackList.get(i).getLat()), Double.parseDouble(rptCarWorkFacility.getLng()), Double.parseDouble(rptCarWorkFacility.getLat()), Double.parseDouble(rptCarWorkFacility.getRadii().toString()));

                                                        if (isInGrid && rptCarWork.getPlanStartTime().compareTo(carTrackList.get(i).getTimestamp()) <= 0 &&
                                                                rptCarWork.getPlanEndTime().compareTo(carTrackList.get(i).getTimestamp()) >= 0) {

                                                            System.out.println(rptCarWork.getId() + "_" + rptCarWorkFacility.getId());
                                                            RptCarWorkDetail rptCarWorkDetail1 = RedisUtil.get(RptCarWorkDetailLastOneKeyToday.token, rptCarWork.getId() + "_" + rptCarWorkFacility.getId(), RptCarWorkDetail.class);

                                                            String uuid;

                                                            if (null != rptCarWorkDetail1 && null != rptCarWorkDetail1.getEndTime() && ((int) (carTrackList.get(i).getTimestamp().getTime() - rptCarWorkDetail1.getEndTime().getTime()) / 1000) < 7200) {
                                                                uuid = rptCarWorkDetail1.getId();
                                                                RptCarWorkDetail rptCarWorkDetail = new RptCarWorkDetail();
                                                                rptCarWorkDetail.setId(rptCarWorkDetail1.getId());
                                                                rptCarWorkDetail.setEndTime(carTrackList.get(i).getTimestamp());
                                                                rptCarWorkDetailMapper.updateById(rptCarWorkDetail);

                                                            } else {
                                                                RptCarWorkDetail rptCarWorkDetail2 = new RptCarWorkDetail();
                                                                uuid = StringUtils.uuid();
                                                                rptCarWorkDetail2.setId(uuid);
                                                                rptCarWorkDetail2.setRptCarWorkId(rptCarWork.getId());
                                                                rptCarWorkDetail2.setCarTrackId(carTrackList.get(i).getId());
                                                                rptCarWorkDetail2.setRptCarWorkFacilityId(rptCarWorkFacility.getId());
                                                                rptCarWorkDetail2.setCarId(rptCarWork.getCarId());
                                                                rptCarWorkDetail2.setCarNumber(rptCarWork.getCarNum());
                                                                rptCarWorkDetail2.setFacilityName(rptCarWorkFacility.getFacilityName());
                                                                rptCarWorkDetail2.setLat(rptCarWorkFacility.getLat());
                                                                rptCarWorkDetail2.setLng(rptCarWorkFacility.getLng());
                                                                rptCarWorkDetail2.setRadii(rptCarWorkFacility.getRadii());
                                                                rptCarWorkDetail2.setWorkDate(DateUtil.format(new Date(), "yyyy-MM-dd"));
                                                                if (null == rptCarWorkDetail2.getStartTime() || "".equals(rptCarWorkDetail2.getStartTime())) {
                                                                    rptCarWorkDetail2.setStartTime(carTrackList.get(i).getTimestamp());
                                                                }
                                                                rptCarWorkDetail2.setEndTime(carTrackList.get(i).getTimestamp());
                                                                rptCarWorkDetailMapper.insert(rptCarWorkDetail2);
                                                            }

                                                            RptCarWorkDetail rptCarWorkDetail = new RptCarWorkDetail();
                                                            rptCarWorkDetail.setId(uuid);
                                                            rptCarWorkDetail.setEndTime(carTrackList.get(i).getTimestamp());

                                                            RedisUtil.set(RptCarWorkDetailLastOneKeyToday.token, rptCarWork.getId() + "_" + rptCarWorkFacility.getId(), rptCarWorkDetail);

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }

            }
        }
    }

    @Override
    public BigDecimal getExpectMileage(CarWorkPlanCountTo carWorkPlanCountTo) {
        BigDecimal mileage = BigDecimal.ZERO;
        List<String> carIds = carWorkPlanCountTo.getCarIds();
        if (null != carIds && !carIds.isEmpty()) {
            QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
            rptCarWorkQueryWrapper.lambda().in(RptCarWork::getCarId, carIds);
            rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
            List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
            for (RptCarWork rptCarWork : rptCarWorkList) {
                mileage = mileage.add(rptCarWork.getWorkMileage());
            }
        }
        BigDecimal aveMileage = mileage.divide(new BigDecimal(7)).setScale(0, BigDecimal.ROUND_UP);

        return aveMileage;
    }

    @Override
    public void initCarAttendance() {
        if (true) {
            try {
                log.info("==================车辆考勤-初始化=================start=");
                /** 车辆考勤 **/
                List<CarPlan> carPlanAllList = new ArrayList<>();

                // 1.查询机械清扫、垃圾清运、垃圾转运作业
                Date date = new Date();
//                Calendar c = Calendar.getInstance();
//                c.setTime(date);
//                c.add(Calendar.DAY_OF_MONTH, 1);
//                date = c.getTime();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                String calendarDate = sdf1.format(date);

                List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
                Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(toList(), workGroups -> workGroups.get(0))));

                List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
                Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

                List<TlOperation> operationList = tlOperationMapper.selectList(new QueryWrapper<>());
                Map<String, TlOperation> operationMap = operationList.stream().collect(Collectors.groupingBy(TlOperation::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


                QueryWrapper<CarPlanCar> carPlanCarQueryWrapper = new QueryWrapper<>();
                List<CarPlanCar> carPlanCarList = carPlanCarMapper.selectList(carPlanCarQueryWrapper);
                Map<String, List<CarPlanCar>> carPlanCarMap = carPlanCarList.stream().collect(Collectors.groupingBy(CarPlanCar::getCarPlanId));

                List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(new QueryWrapper<>());
                Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

                QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
                List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
                Map<String, List<CarPlanCollectPoint>> carPlanCollectPointMap = carPlanCollectPointList.stream().collect(Collectors.groupingBy(CarPlanCollectPoint::getCarPlanId));

                QueryWrapper<CarPlanCollectStand> carPlanCollectStandQueryWrapper = new QueryWrapper<>();
                List<CarPlanCollectStand> carPlanCollectStandList = carPlanCollectStandMapper.selectList(carPlanCollectStandQueryWrapper);
                Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap = carPlanCollectStandList.stream().collect(Collectors.groupingBy(CarPlanCollectStand::getCarPlanId));

                QueryWrapper<CarPlanProcessStand> carPlanProcessStandQueryWrapper = new QueryWrapper<>();
                List<CarPlanProcessStand> carPlanProcessStandList = carPlanProcessStandMapper.selectList(carPlanProcessStandQueryWrapper);
                Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap = carPlanProcessStandList.stream().collect(Collectors.groupingBy(CarPlanProcessStand::getCarPlanId));


                QueryWrapper<CarPlan> carPlanQueryWrapper = new QueryWrapper<>();
                carPlanQueryWrapper.lambda().le(CarPlan::getWorkStartDate, calendarDate);
                carPlanQueryWrapper.lambda().ge(CarPlan::getWorkEndDate, calendarDate);
//                carPlanQueryWrapper.lambda().eq(CarPlan::getType, 2);
                carPlanQueryWrapper.lambda().eq(CarPlan::getIsEnable, "1");
                List<CarPlan> carPlanList = carPlanMapper.selectList(carPlanQueryWrapper);
                for (CarPlan carPlan : carPlanList) {
                    boolean exist = TimeUtil.checkPeriodDate(DateUtil.parse(calendarDate), StringUtils.StringToDate3(carPlan.getWorkStartDate()), StringUtils.StringToDate3(carPlan.getWorkEndDate()), carPlan.getWorkCycle());
                    if (exist) {
                        carPlanAllList.add(carPlan);
                    }
                }

                for (CarPlan carPlan : carPlanAllList) {
                    List<String> carIds = new ArrayList<>();
                    if (carPlan.getType() == 1) {
//                        List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
//                        if (null != carPlanCarList1) {
//                            for (CarPlanCar carPlanCar : carPlanCarList1) {
//                                carIds.add(carPlanCar.getCarId());
//                            }
//                        }
//                        //如果可以查询车辆id 说明有排班
//                        List<TlCar> tlCarShiftList = tlCarMapper.selectInIdCarIds(carIds);
//                        for (TlCar tlCar : tlCarShiftList) {
//                            tlCar.setShiftStatus(0);
//                            tlCarMapper.updateById(tlCar);
//                        }
//
//                        if (null == carIds || carIds.isEmpty()) {
//                            continue;
//                        }
//                        Grid grid = gridMapper.selectById(carPlan.getGridId());
//                        if (null == grid) {
//                            log.error("---不存在此网格，不初始化车辆考勤表，gridId:{},carPlanId:{}，carIdsStr:{}", carPlan.getGridId(), carPlan.getId(), String.join(",", carIds));
//                            continue;
//                        }
//                        RptCarWork rptCarWork = new RptCarWork();
//                        rptCarWork.setPlanId(carPlan.getId());
//                        rptCarWork.setWorkCycle(carPlan.getWorkCycle());
//                        rptCarWork.setPlanName(carPlan.getPlanName());
//                        rptCarWork.setGridId(carPlan.getGridId());
//                        rptCarWork.setGridName(grid.getName());
//                        WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
//                        if (null != workGroup) {
//                            rptCarWork.setGroupId(workGroup.getWorkGroupId());
//                            rptCarWork.setGroupName(workGroup.getName());
//                        }
//                        WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
//                        if (null != workRule) {
//                            rptCarWork.setRuleId(workRule.getWorkRuleId());
//                            rptCarWork.setRuleName(workRule.getRuleName());
//                        }
//                        rptCarWork.setExpectOilConsumption(null == carPlan.getExpectOilConsumption() ? BigDecimal.ZERO : carPlan.getExpectOilConsumption());
//                        rptCarWork.setExpectWaterConsumption(null == carPlan.getExpectWaterConsumption() ? BigDecimal.ZERO : carPlan.getExpectWaterConsumption());
//                        rptCarWork.setExpectWorkTime(null == carPlan.getExpectTimeConsumption() ? BigDecimal.ZERO : carPlan.getExpectTimeConsumption());
//                        rptCarWork.setWorkType(2);
//                        //计划总进度，机械清扫为总里程，其他为收集点数量
//                        Double km = null == grid.getKm() ? 0 : grid.getKm();
//                        Integer tangCi = null == carPlan.getWorkTrip() ? 0 : carPlan.getWorkTrip();
//                        rptCarWork.setAllProgress(km.intValue() * tangCi);
//                        TlOperation tlOperation = operationMap.get(carPlan.getWorkTypeId());
//                        if (null != tlOperation) {
//                            rptCarWork.setWorkTypeId(tlOperation.getId());
//                            rptCarWork.setWorkTypeName(tlOperation.getOperationtype());
//                        }
//                        rptCarWork.setCarWorkType(carPlan.getWorkType());
//                        rptCarWork.setTangCi(tangCi);
//                        handlePlanAndSave(DateUtil.parse(calendarDate), rptCarWork, carPlan.getWorkGroupId(), carIds, tlFacilitiesMap, carPlanCollectPointMap, carPlanCollectStandMap, carPlanProcessStandMap);
                    } else if (carPlan.getType() == 2) {

                        //垃圾清运
//                        List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
//                        if (null != carPlanCarList1) {
//                            for (CarPlanCar carPlanCar : carPlanCarList1) {
//                                carIds.add(carPlanCar.getCarId());
//                            }
//                        }
//                        List<TlCar> tlCarShiftList = tlCarMapper.selectInIdCarIds(carIds);
//                        for (TlCar tlCar : tlCarShiftList) {
//                            tlCar.setShiftStatus(0);
//                            tlCarMapper.updateById(tlCar);
//                        }
//                        if (null == carIds || carIds.isEmpty()) {
//                            continue;
//                        }
//                        Grid grid = gridMapper.selectById(carPlan.getGridId());
//                        if (null == grid) {
//                            log.error("---不存在此网格，不初始化车辆考勤表，gridId:{},carPlanId:{}，carIdsStr:{}", carPlan.getGridId(), carPlan.getId(), String.join(",", carIds));
//                            continue;
//                        }
//                        RptCarWork rptCarWork = new RptCarWork();
//                        rptCarWork.setPlanId(carPlan.getId());
//                        rptCarWork.setWorkCycle(carPlan.getWorkCycle());
//
//                        rptCarWork.setPlanName(carPlan.getPlanName());
//                        rptCarWork.setGridId(carPlan.getGridId());
//                        rptCarWork.setGridName(grid.getName());
//                        WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
//                        if (null != workGroup) {
//                            rptCarWork.setGroupId(workGroup.getWorkGroupId());
//                            rptCarWork.setGroupName(workGroup.getName());
//                        }
//                        WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
//                        if (null != workRule) {
//                            rptCarWork.setRuleId(workRule.getWorkRuleId());
//                            rptCarWork.setRuleName(workRule.getRuleName());
//                        }
//                        rptCarWork.setExpectWorkTime(null == carPlan.getExpectTimeConsumption() ? BigDecimal.ZERO : carPlan.getExpectTimeConsumption());
//                        rptCarWork.setWorkType(3);
//                        handlePlanAndSave(DateUtil.parse(calendarDate), rptCarWork, carPlan.getWorkGroupId(), carIds, tlFacilitiesMap, carPlanCollectPointMap, carPlanCollectStandMap, carPlanProcessStandMap);
                    } else {
                        //垃圾转运
                        List<CarPlanCar> carPlanCarList1 = carPlanCarMap.get(carPlan.getId());
                        if (null != carPlanCarList1) {
                            for (CarPlanCar carPlanCar : carPlanCarList1) {
                                carIds.add(carPlanCar.getCarId());
                            }
                        }
                        List<TlCar> tlCarShiftList = tlCarMapper.selectInIdCarIds(carIds);
                        for (TlCar tlCar : tlCarShiftList) {
                            tlCar.setShiftStatus(0);
                            tlCarMapper.updateById(tlCar);
                        }
                        if (null == carIds || carIds.isEmpty()) {
                            continue;
                        }
                        RptCarWork rptCarWork = new RptCarWork();
                        rptCarWork.setId(StringUtils.uuid());
                        rptCarWork.setPlanId(carPlan.getId());
                        rptCarWork.setPlanName(carPlan.getPlanName());
                        WorkGroup workGroup = workGroupMap.get(carPlan.getWorkGroupId());
                        if (null != workGroup) {
                            rptCarWork.setGroupId(workGroup.getWorkGroupId());
                            rptCarWork.setGroupName(workGroup.getName());
                        }
                        WorkRule workRule = workRuleMap.get(carPlan.getWorkRuleId());
                        if (null != workRule) {
                            rptCarWork.setRuleId(workRule.getWorkRuleId());
                            rptCarWork.setRuleName(workRule.getRuleName());
                        }
                        rptCarWork.setExpectMileage(null == carPlan.getExpectMileage() ? BigDecimal.ZERO : carPlan.getExpectMileage());
                        rptCarWork.setWorkType(4);
                        handlePlanAndSave(DateUtil.parse(calendarDate), rptCarWork, carPlan.getWorkGroupId(), carIds, tlFacilitiesMap, carPlanCollectPointMap, carPlanCollectStandMap, carPlanProcessStandMap);
                    }

                }
            } catch (ParseException e) {
                e.printStackTrace();
                log.error("ERROR", e);
            }
        }
        log.info("==================车辆考勤-初始化=================end=");
    }


    private void handlePlanAndSave(Date now, RptCarWork rptCarWork, String workGroupId, List<String> carIds,
                                   Map<String, TlFacilities> tlFacilitiesMap, Map<String, List<CarPlanCollectPoint>> carPlanCollectPointMap,
                                   Map<String, List<CarPlanCollectStand>> carPlanCollectStandMap, Map<String, List<CarPlanProcessStand>> carPlanProcessStandMap) {

        WorkGroup workGroup = workGroupMapper.selectById(workGroupId);
        if (null != workGroup) {
            //勤类型，固定班1，冬夏班2
            Integer kaoqinType = workGroup.getKaoqinType();
            //1:夏季,2:冬季
            String season = DateUtils.getCurrentSeason(now);
            String kaoqinIds = workGroup.getKaoqinIds();
            if (StringUtils.isEmpty(kaoqinIds)) {
                return;
            }
            List<WorkKaoqin> workKaoqinList = workKaoqinMapper.selectBatchIds(Arrays.asList(kaoqinIds.split(",")));
            for (String carId : carIds) {
                String workDate = DateUtil.format(now, "yyyy-MM-dd");

                //重复判断
                QueryWrapper<RptCarWork> rptCarWrokWrapper = new QueryWrapper<>();
                rptCarWrokWrapper.lambda().eq(RptCarWork::getCarId, carId);
                rptCarWrokWrapper.lambda().eq(RptCarWork::getPlanId, rptCarWork.getPlanId());
                rptCarWrokWrapper.lambda().eq(RptCarWork::getWorkDate, workDate);
                List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWrokWrapper);
                if (!CollectionUtils.isEmpty(rptCarWorkList)) {
                    continue;
                }

                TlCar tlCar = tlCarMapper.selectByIdWithCarType(carId);
                if (null == tlCar) {
                    continue;
                }
                for (WorkKaoqin workKaoqin : workKaoqinList) {
                    try {
                        String planStartTime = null;//排班开始时间
                        String planEndTime = null;//排班结束时间
                        if (1 == kaoqinType) {//固定班
                            planStartTime = workKaoqin.getSbsj();
                            planEndTime = workKaoqin.getXbsj();
                        } else if (2 == kaoqinType) {//冬夏班
                            if ("summer".equals(season)) {
                                planStartTime = workKaoqin.getXsbsj();
                                planEndTime = workKaoqin.getXxbsj();
                            } else if ("winter".equals(season)) {
                                planStartTime = workKaoqin.getDsbsj();
                                planEndTime = workKaoqin.getDxbsj();
                            }
                        }
                        rptCarWork.setId(StringUtils.uuid());
                        rptCarWork.setCarId(carId);
                        rptCarWork.setCarNum(tlCar.getCarnumber());
                        rptCarWork.setCarType(tlCar.getCarTypeName());
                        rptCarWork.setWorkDate(workDate);//排班日期
                        rptCarWork.setWorkMonth(DateUtil.format(now, "yyyy-MM"));//排班月份
                        rptCarWork.setDepartmentId(tlCar.getDeptid());
                        System.out.println(workDate + " " + planStartTime);
                        if (null != planStartTime) {
                            rptCarWork.setPlanStartTime(DateUtil.parse(workDate + " " + planStartTime, "yyyy-MM-dd HH:mm"));
                        }
                        if (null != planEndTime) {
                            rptCarWork.setPlanEndTime(DateUtil.parse(workDate + " " + planEndTime, "yyyy-MM-dd HH:mm"));
                        }
                        rptCarWork.setCreateTime(new Date());
                        rptCarWork.setCarName(tlCar.getCarnumber() + "[" + tlCar.getCarTypeName() + "]");
                        rptCarWork.setShiftId(workKaoqin.getWorkKaoqinId());
                        rptCarWork.setShiftName(workKaoqin.getName());
//                        rptCarWork.set
                        // 保存车辆考勤

                        //生成从表
                        if (rptCarWork.getWorkType() == 3) {
                            List<String> carPlanCollectPointIds = new ArrayList<>();
                            List<CarPlanCollectPoint> carPlanCollectPointList1 = carPlanCollectPointMap.get(rptCarWork.getPlanId());
                            if (null != carPlanCollectPointList1) {
                                for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList1) {
                                    carPlanCollectPointIds.add(carPlanCollectPoint.getCollectPointId());
                                }
                            }
                            if (!carPlanCollectPointIds.isEmpty()) {
                                for (String carPlanCollectPointId : carPlanCollectPointIds) {
                                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectPointId);
                                    if (null != tlFacilities) {
                                        RptCarWorkFacility rptCarWorkFacility = new RptCarWorkFacility();
                                        rptCarWorkFacility.setId(StringUtils.uuid());
                                        rptCarWorkFacility.setRptCarWorkId(rptCarWork.getId());
                                        rptCarWorkFacility.setCollectPointId(tlFacilities.getId());
                                        rptCarWorkFacility.setFacilityName(tlFacilities.getFaclname());
                                        String[] facAddr = tlFacilities.getFacladdr().split(",");
                                        String lng = facAddr[0];
                                        String lat = facAddr[1];
                                        rptCarWorkFacility.setLat(lat);
                                        rptCarWorkFacility.setLng(lng);
                                        rptCarWorkFacility.setWorkType(1);
                                        rptCarWorkFacility.setRadii(tlFacilities.getRadii());
                                        rptCarWorkFacilityMapper.insert(rptCarWorkFacility);
                                    }
                                }
                                rptCarWork.setSjdIds(String.join(",", carPlanCollectPointIds));
                                rptCarWork.setAllProgress(carPlanCollectPointIds.size());
                            }
                        } else if (rptCarWork.getWorkType() == 4) {

                            List<CarPlanCollectStand> carPlanCollectStandList1 = carPlanCollectStandMap.get(rptCarWork.getPlanId());
                            if (null != carPlanCollectStandList1) {
                                for (CarPlanCollectStand carPlanCollectStand : carPlanCollectStandList1) {
                                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectStand.getCollectStandId());
                                    if (null != tlFacilities) {
                                        RptCarWorkFacility rptCarWorkFacility = new RptCarWorkFacility();
                                        rptCarWorkFacility.setId(StringUtils.uuid());
                                        rptCarWorkFacility.setRptCarWorkId(rptCarWork.getId());
                                        rptCarWorkFacility.setCollectStandId(tlFacilities.getId());
                                        rptCarWorkFacility.setFacilityName(tlFacilities.getFaclname());
                                        String[] facAddr = tlFacilities.getFacladdr().split(",");
                                        String lng = facAddr[0];
                                        String lat = facAddr[1];
                                        rptCarWorkFacility.setLat(lat);
                                        rptCarWorkFacility.setLng(lng);
                                        rptCarWorkFacility.setRadii(tlFacilities.getRadii());
                                        rptCarWorkFacility.setWorkType(2);
                                        rptCarWorkFacilityMapper.insert(rptCarWorkFacility);
                                    }
                                }
                            }
                            List<CarPlanProcessStand> carPlanProcessStandList1 = carPlanProcessStandMap.get(rptCarWork.getPlanId());
                            if (null != carPlanProcessStandList1) {
                                for (CarPlanProcessStand carPlanProcessStand : carPlanProcessStandList1) {
                                    TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanProcessStand.getProcessStandId());
                                    if (null != tlFacilities) {
                                        RptCarWorkFacility rptCarWorkFacility = new RptCarWorkFacility();
                                        rptCarWorkFacility.setId(StringUtils.uuid());
                                        rptCarWorkFacility.setRptCarWorkId(rptCarWork.getId());
                                        rptCarWorkFacility.setProcessStandId(tlFacilities.getId());
                                        rptCarWorkFacility.setFacilityName(tlFacilities.getFaclname());
                                        String[] facAddr = tlFacilities.getFacladdr().split(",");
                                        String lng = facAddr[0];
                                        String lat = facAddr[1];
                                        rptCarWorkFacility.setLat(lat);
                                        rptCarWorkFacility.setLng(lng);
                                        rptCarWorkFacility.setRadii(tlFacilities.getRadii());
                                        rptCarWorkFacility.setWorkType(3);
                                        rptCarWorkFacilityMapper.insert(rptCarWorkFacility);
                                    }
                                }
                            }
                        }
                        rptCarWorkMapper.insert(rptCarWork);

                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("ERROR", e);
                    }
                }
            }
        }
    }


    @Override
    public void mileageComplete(CarWorkPlanCountTo carWorkPlanCountTo) {

        long carStartTime = System.currentTimeMillis();
        log.info("===================机械作业、垃圾清运、垃圾转运-里程完成度，任务开始===============");
//        Date now = new Date();
        //遍历 车辆考勤，根据条件获取当天有作业的车辆，查询车辆的工作里程，更新车辆考勤表
        QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
        carWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 2);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        for (RptCarWork rptCarWork : rptCarWorkList) {
            String gridId = rptCarWork.getGridId();
            //作业类型：2机械清扫；3垃圾清运；4垃圾转运
            Integer workType = rptCarWork.getWorkType();
            Integer allProgress = rptCarWork.getAllProgress();
            if (null == allProgress || allProgress <= 0) {
                log.error("网格没有配置里程，gridId:{}", gridId);
                continue;
            }
            Integer nowProgress = rptCarWork.getWorkMileage().intValue();
            // 获取阀值
            Integer jobSetId = workType + 1;
            Jobset jobset = jobsetMapper.selectById(jobSetId);
            Double kmOkThreshold;
            kmOkThreshold = jobset.getKmOkThreshold();
//            if (null != rptCarWork.getWorkStartTime() && null != rptCarWork.getWorkEndTime()) {
//                if (rptCarWork.getWorkStartTime().compareTo(rptCarWork.getPlanStartTime()) >= 0 && rptCarWork.getWorkEndTime().compareTo(rptCarWork.getPlanEndTime()) <= 0 && rptCarWork.getStatus() == 0) {
//                    rptCarWork.setStatus(1);//进行中：当日排班进行中；
//                }
//            }
//            if (rptCarWork.getStatus() == 1 || rptCarWork.getStatus() == 0) {
            if (nowProgress > 0) {
                /** 更新 完成状态 和 当前进度 **/
                if (nowProgress > allProgress) {
                    nowProgress = allProgress;
                }
                Integer completePercent = nowProgress * 100 / allProgress;
                // 完成状态
//                    if (null != rptCarWork.getWorkEndTime() && rptCarWork.getWorkEndTime().compareTo(rptCarWork.getPlanEndTime()) <= 0) {
//                        if (completePercent >= kmOkThreshold) {
//                            rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
//                        }
//                    } else {
                if (completePercent < kmOkThreshold) {
                    rptCarWork.setStatus(2);//未完成：考勤时间结束，未完成作业；
                } else {
                    rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                }
//                    }
                // 当前里程
                rptCarWork.setWorkMileage(new BigDecimal(nowProgress));
                // 完成百分比
                rptCarWork.setCompletePercent(completePercent);
            } else {
                rptCarWork.setStatus(0);
            }
            rptCarWorkMapper.updateById(rptCarWork);
//            }
        }
        long carEndTime = System.currentTimeMillis();
        log.info("===================机械作业、垃圾清运、垃圾转运-里程完成度，任务结束===============执行时间(毫秒)：{}", carEndTime - carStartTime);
    }

    @Override
    public void updateTrashPickup(String workDate) {
        Date now = new Date();

        QueryWrapper<RptCarWorkFacility> rptCarWorkFacilityQueryWrapper = new QueryWrapper<>();
        rptCarWorkFacilityQueryWrapper.lambda().eq(RptCarWorkFacility::getWorkDate, DateUtil.format(new Date(), "yyyy-MM-dd"));
        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(rptCarWorkFacilityQueryWrapper);
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityListMap = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));

        //遍历 车辆考勤，根据条件获取当天有作业的车辆，查询车辆的工作里程，更新车辆考勤表
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, workDate);
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        for (RptCarWork rptCarWork : rptCarWorkList) {
            if (now.compareTo(rptCarWork.getPlanStartTime()) >= 0 && now.compareTo(rptCarWork.getPlanEndTime()) <= 0 && rptCarWork.getStatus() == 0) {
                rptCarWork.setStatus(1);//进行中：当日排班进行中；
            }
            if (StringUtils.isNotEmpty(rptCarWork.getSjdIds())) {
                String[] sjdIds = rptCarWork.getSjdIds().split(",");

                List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityListMap.get(rptCarWork.getId());
                if (null != rptCarWorkFacilityList1) {
                    Set cleanFaclIds = new HashSet();
                    for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                        String collectPointId = rptCarWorkFacility.getCollectPointId();
                        Integer cleanType = rptCarWorkFacility.getCleanType();
                        if (null != cleanType && cleanType == 1) {
                            cleanFaclIds.add(collectPointId);
                        }
                    }
                    if (sjdIds.length > 0) {
                        Double pointOkThreshold = 0d;
                        Jobset jobset = jobsetMapper.selectById('4');
                        if (null != jobset) {
                            pointOkThreshold = jobset.getPointOkThreshold();
                        }
                        BigDecimal finishRate = (new BigDecimal(cleanFaclIds.size()).divide(new BigDecimal(sjdIds.length), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_UP));
                        if (now.compareTo(rptCarWork.getPlanEndTime()) <= 0) {
                            if (finishRate.compareTo(new BigDecimal(pointOkThreshold)) >= 0) {
                                rptCarWork.setStatus(3);//考勤时间结束，已完成作业。
                            }
                        } else {
                            if (finishRate.compareTo(new BigDecimal(pointOkThreshold)) < 0) {
                                rptCarWork.setStatus(2);//未完成：考勤时间结束，未完成作业；
                            }
                        }
                        if (finishRate.compareTo(new BigDecimal(100)) >= 0) {
                            finishRate = new BigDecimal(100);
                        }
                        // 完成百分比
                        rptCarWork.setCompletePercent(finishRate.intValue());
                        // 当前里程
                        rptCarWork.setPickupPoint(cleanFaclIds.size());

                        rptCarWork.setAllProgress(sjdIds.length);
                        // 今日里程
                        rptCarWork.setTodayMileage(sjdIds.length);

                        rptCarWorkMapper.updateById(rptCarWork);
                    }
                }
            }
        }
    }

    @Override
    public void createRptCarWork(CarWorkPlanCountTo carWorkPlanCountTo) {

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        rptCarWorkQueryWrapper.lambda().between(RptCarWork::getWorkDate, carWorkPlanCountTo.getStartDate(), carWorkPlanCountTo.getEndDate());
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);

        QueryWrapper<CarPlanCollectPoint> carPlanCollectPointQueryWrapper = new QueryWrapper<>();
        List<CarPlanCollectPoint> carPlanCollectPointList = carPlanCollectPointMapper.selectList(carPlanCollectPointQueryWrapper);
        Map<String, List<CarPlanCollectPoint>> carPlanCollectPointMap = carPlanCollectPointList.stream().collect(Collectors.groupingBy(CarPlanCollectPoint::getCarPlanId));

        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));


        for (RptCarWork rptCarWork : rptCarWorkList) {

            List<CarPlanCollectPoint> carPlanCollectPointList1 = carPlanCollectPointMap.get(rptCarWork.getPlanId());

            for (CarPlanCollectPoint carPlanCollectPoint : carPlanCollectPointList1) {
                TlFacilities tlFacilities = tlFacilitiesMap.get(carPlanCollectPoint.getCollectPointId());
                if (null != tlFacilities) {
                    RptCarWorkFacility rptCarWorkFacility = new RptCarWorkFacility();
                    rptCarWorkFacility.setId(StringUtils.uuid());
                    rptCarWorkFacility.setRptCarWorkId(rptCarWork.getId());
                    rptCarWorkFacility.setCollectPointId(tlFacilities.getId());
                    rptCarWorkFacility.setFacilityName(tlFacilities.getFaclname());
                    rptCarWorkFacility.setCarId(rptCarWork.getCarId());
                    rptCarWorkFacility.setWorkDate(rptCarWork.getWorkDate());
                    String[] facAddr = tlFacilities.getFacladdr().split(",");
                    String lng = facAddr[0];
                    String lat = facAddr[1];
                    rptCarWorkFacility.setLat(lat);
                    rptCarWorkFacility.setLng(lng);
                    rptCarWorkFacility.setWorkType(1);
                    rptCarWorkFacility.setRadii(tlFacilities.getRadii());
                    rptCarWorkFacilityMapper.insert(rptCarWorkFacility);
                }
            }


        }


    }

    @Override
    public void updateCleanType(CarWorkPlanCountTo carWorkPlanCountTo) {

        QueryWrapper<RptCarWorkFacility> rptCarWorkFacilityQueryWrapper = new QueryWrapper<>();
        rptCarWorkFacilityQueryWrapper.lambda().eq(RptCarWorkFacility::getWorkDate, carWorkPlanCountTo.getDate());
        List<RptCarWorkFacility> rptCarWorkFacilityList = rptCarWorkFacilityMapper.selectList(rptCarWorkFacilityQueryWrapper);
        Map<String, List<RptCarWorkFacility>> rptCarWorkFacilityListMap = rptCarWorkFacilityList.stream().filter(rptCarWorkFacility -> null != rptCarWorkFacility.getRptCarWorkId()).collect(Collectors.groupingBy(RptCarWorkFacility::getRptCarWorkId));


        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, carWorkPlanCountTo.getDate());
        carTrackQueryWrapper.lambda().isNotNull(CarTrack::getCarId);
        carTrackQueryWrapper.lambda().eq(CarTrack::getJobType, 3);
        carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
        List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrapper);

//        Map<String, List<CarTrack>> carTrackMap = carTrackList.stream().collect(Collectors.groupingBy(CarTrack::getCarId));


        QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, carWorkPlanCountTo.getDate());
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkType, 3);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        Map<String, List<RptCarWork>> rptCarWorkMap = rptCarWorkList.stream().collect(Collectors.groupingBy(RptCarWork::getCarId));


        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesList.stream().collect(Collectors.groupingBy(TlFacilities::getId, Collectors.collectingAndThen(toList(), values -> values.get(0))));

        for (CarTrack carTrack : carTrackList) {
            List<RptCarWork> rptCarWorkList1 = rptCarWorkMap.get(carTrack.getCarId());
            if (null != rptCarWorkList1) {
                for (RptCarWork rptCarWork : rptCarWorkList1) {
                    List<RptCarWorkFacility> rptCarWorkFacilityList1 = rptCarWorkFacilityListMap.get(rptCarWork.getId());
                    if (null != rptCarWorkFacilityList1) {
                        for (RptCarWorkFacility rptCarWorkFacility : rptCarWorkFacilityList1) {
                            TlFacilities tlFacilities = tlFacilitiesMap.get(rptCarWorkFacility.getCollectPointId());
                            if (null != tlFacilities) {
                                String[] faclAddr = tlFacilities.getFacladdr().split(",");
                                String lngAddr = faclAddr[0];
                                String latAddr = faclAddr[1];
                                Boolean flag = DistanceUtils.insideCircle(Double.parseDouble(carTrack.getLng()), Double.parseDouble(carTrack.getLat()), Double.parseDouble(lngAddr), Double.parseDouble(latAddr), Double.parseDouble(tlFacilities.getRadii().toString()));
                                if (flag) {
                                    rptCarWorkFacility.setCarTrackId(carTrack.getId());
                                    rptCarWorkFacility.setCleanType(1);
                                    rptCarWorkFacilityMapper.updateById(rptCarWorkFacility);
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
